입력
4 5
00110
00011
11111
00000
출력
3
입력
15 14
00000111100000
11111101111110
11011101101110
11011101100000
11011111111111
11011111111100
11000000011111
01111111111111
00000000011111
01111111111000
00011111111000
00000001111000
11111111110011
11100011111111
11100011111111
출력
8
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int n, m;
int v[1000][1000];
bool dfs(int y, int x)
{
if (y < 0 || y >= n
|| x < 0 || x >= m)
return false;
if (v[y][x] == 1)
{
return false;
}
else
{
v[y][x] = 1;
dfs(y + 1, x);
dfs(y - 1, x);
dfs(y, x + 1);
dfs(y, x - 1);
}
return true;
}
int main() {
int cnt = 0;
cin >> n >> m;
vector<string>word(n);
for (int i = 0; i < n; i++)
{
cin >> word[i];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
v[i][j] = word[i][j] - '0';
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (dfs(i, j))
cnt++;
}
}
cout << cnt;
return 0;
}