#include #include #include #include #include using namespace std; int H,W; bool A[10000][10000]; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; int solve(int y,int x){ if (!A[y][x]) { return 0; } A[y][x] = false; queue > q; q.push(make_pair(y,x)); while(!q.empty()) { int c_y = q.front().first; int c_x = q.front().second; q.pop(); A[c_y][c_x] = false; for (int k = 0; k < 4; k++) { int ny = c_y + dy[k]; int nx = c_x + dx[k]; if (0 <= ny && ny < H && 0 <= nx && nx < W && A[ny][nx]) { // solve(ny, nx); A[ny][nx] = false; q.push(make_pair(ny, nx)); } } } return 1; } int main(){ cin >> H >> W; for(int i = 0;i < H;i++) { for(int j = 0;j < W;j++) { cin >> A[i][j]; } } int res = 0; for(int i = 0;i < H;i++) { for(int j = 0;j < W;j++) { res += solve(i,j); } } cout << res << endl; return 0; }