#include #include #include template std::vector vec(int len, T elem) { return std::vector(len, elem); } const std::vector dx{-1, 0, 1, 0}, dy{0, -1, 0, 1}; void solve() { int h, w; std::cin >> h >> w; auto grid = vec(h, vec(w, 0)); for (auto& v : grid) { for (auto& x : v) { std::cin >> x; } } int ans = 0; for (int sx = 0; sx < h; ++sx) { for (int sy = 0; sy < w; ++sy) { if (grid[sx][sy] == 0) continue; ++ans; grid[sx][sy] = 0; std::queue> que; que.emplace(sx, sy); while (!que.empty()) { auto [x, y] = que.front(); que.pop(); for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || h <= nx || ny < 0 || w <= ny || grid[nx][ny] == 0) continue; grid[nx][ny] = 0; que.emplace(nx, ny); } } } } std::cout << ans << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }