#include #include #include using namespace std; int main() { int H, W; cin >> H >> W; vector> A(H, vector(W)); for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { cin >> A[h][w]; } } int ans = 0; for (int h = 0; h < H; ++h) { for (int w = 0; w < W; ++w) { if (A[h][w]) { queue> q; q.push({h, w}); A[h][w] = 0; while (!q.empty()) { int hh = q.front().first; int ww = q.front().second; q.pop(); vector> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; for (const auto& dir : directions) { int dh = dir.first; int dw = dir.second; if (0 <= hh + dh && hh + dh < H && 0 <= ww + dw && ww + dw < W && A[hh + dh][ww + dw]) { A[hh + dh][ww + dw] = 0; q.push({hh + dh, ww + dw}); } } } ans++; } } } cout << ans << endl; return 0; }