#include #include using namespace std; queue< pair > Q; int H, W; int A[3010][3010]; int arrive[3010][3010]; int dy[4] = { 1, 0, -1, 0 }, dx[4] = { 0, 1, 0, -1 }; int bfs(int y, int x){ if(arrive[y][x] == 2) return 0; Q.push(make_pair(y, x)); int v, w; while(!Q.empty()){ v = Q.front().first; w = Q.front().second; arrive[v][w] = 2; Q.pop(); for(int i = 0; i < 4; i++){ int ny = v + dy[i]; int nx = w + dx[i]; if(ny < 0 || ny >= H || nx < 0 || nx >= W || arrive[ny][nx] != 0 || A[ny][nx]==0){ continue; } Q.push(make_pair(ny, nx)); arrive[ny][nx] = 1; } } return 1; } int solve() { int ans = 0; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++){ if(A[i][j] == 1) ans += bfs(i, j); } } return ans; } int main(void){ // Your code here! cin >> H >> W; for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) cin >> A[i][j]; for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) arrive[i][j] = 0; cout << solve() << endl; }