AAAAAZBX @ 2023-04-06 11:29:08
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1002;
int n, ans;
char c;
int map[N][N];
bool st[N][N];
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
void dfs(int x1, int y1)
{
st[x1][y1] = 1;
for (int k = 0; k < 4; k++)
{
int x = x1 + dx[k], y = y1 + dy[k];
if (!st[x][y] && map[x][y] == 1 && x >= 1 && x <= n && y >= 1 && y <= n)
dfs(x, y);
}
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cin >> c;
map[i][j] = (c == '#');
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (!st[i][j] && map[i][j])
{
ans++;
dfs(i, j);
}
memset(st, 0, sizeof st);
for (int i = 0; i <= n + 1; i++)
{
map[0][i] = 1;
map[i][0] = 1;
map[n + 1][0] = 1;
map[0][n + 1] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (map[i][j] == 1)
{
for (int k = 0; k < 4; k++)
{
int x = i + dx[k], y = j + dy[k];
if (x >= 1 && x <= n && y >= 1 && y <= n && map[x][y] == 0)map[i][j] = 2;
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (!st[i][j] && map[i][j] == 1)
{
ans--;
dfs(i, j);
}
printf("%d", ans);
return 0;
}