#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using P = pair; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } constexpr int dx[4] = {1, 0, -1, 0}; constexpr int dy[4] = {0, 1, 0, -1}; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int H, W; cin >> H >> W; vector> A(H, vector(W)); for(int i = 0; i < H; ++i){ for(int j = 0; j < W; ++j){ cin >> A[i][j]; } } vector> used(H, vector(W)); int ans = 0; for(int i = 0; i < H; ++i){ for(int j = 0; j < W; ++j){ if(used[i][j] || A[i][j] == 0) continue; ans += 1; queue

que; que.emplace(i, j); used[i][j] = true; while(!que.empty()){ auto [x, y] = que.front(); que.pop(); for(int k = 0; k < 4; ++k){ int nx = x + dx[k], ny = y + dy[k]; if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue; if(used[nx][ny] || A[nx][ny] == 0) continue; que.emplace(nx, ny); used[nx][ny] = true; } } } } cout << ans << '\n'; return 0; }