#include using namespace std; bool A[10000000]; int comp[10000000]; int main() { int H, W; cin >> H >> W; H += 2; W += 2; for (int i = 1; i <= H - 2; i++) { for (int j = 1; j <= W - 2; j++) { cin >> A[W * i + j]; } } int n = 1; for (int i = 0; i < H * W; i++) { if ((not A[i]) or (comp[i])) { continue; } comp[i] = n; queue q; q.push(i); while (!q.empty()) { int v = q.front(); q.pop(); int dx[] = {1, -1, W, -W}; for (i = 0; i < 4; i++) { int w = v + dx[i]; if ((not A[w]) or (comp[w])) { continue; } comp[w] = n; q.push(w); } } n++; } cout << n - 1 << endl; }