#include using namespace std; signed main() { ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector> G(N, vector(M)); for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) cin >> G[i][j]; vector fa(N * M); iota(fa.begin(), fa.end(), 0); function find = [&](int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }; for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) if (G[i][j]) for (int dx = -1; dx < 2; ++dx) for (int dy = -1; dy < 2; ++dy) if (abs(dx) + abs(dy) == 1) { int ni = i + dx; int nj = j + dy; if (ni < 0 || N <= ni || nj < 0 || M <= nj) continue; if (G[ni][nj]) fa[find(ni * M + nj)] = find(i * M + j); } int ans = 0; for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) if (G[i][j]) ans += find(i * M + j) == i * M + j; cout << ans << endl; }