#include #include #include using namespace std; vector field(90000001, 0); void dfs(int row, int col, int H, int W) { field[row * W + col] = 0; if((row - 1 >= 0) && (field[(row - 1) * W + col] == 1)) { dfs(row - 1, col, H, W); } if((row + 1 < H) && (field[(row + 1) * W + col] == 1)) { dfs(row + 1, col, H, W); } if((col - 1 >= 0) && (field[row * W + col - 1] == 1)) { dfs(row, col - 1, H, W); } if((col + 1 < W) && (field[row * W + col + 1] == 1)) { dfs(row, col + 1, H, W); } } int main(void) { int H, W; cin >> H >> W; for(int i = 0; i < H * W; i++) { cin >> field[i]; } int ans = 0; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { if(1 == field[i * W + j]) { dfs(i, j, H, W); ans++; } } } cout << ans << endl; return 0; }