import std; void main() { int H, W; readf("%d %d\n", H, W); auto A = new bool[][](H, W); foreach (i; 0 .. H) { auto tmp = readln.chomp.split.to!(int[]); foreach (j; 0 .. W) { A[i][j] = tmp[j].to!bool; } } auto deltaX = [-1, 0, 1, 0], deltaY = [0, 1, 0, -1]; int res; foreach (i; 0 .. H) { foreach (j; 0 .. W) { if (!A[i][j]) { continue; } ++res; int[] que; que ~= i * W + j; while (!que.empty) { auto f = que.front; que.popFront; int x = f / W; int y = f % W; A[x][y] = false; foreach (dx, dy; zip(deltaX, deltaY)) { auto nx = x, ny = y; nx += dx, ny += dy; if (nx < 0 || nx >= H || ny < 0 || ny >= W) { continue; } if (!A[nx][ny]) { continue; } que ~= nx * W + ny; } } } } res.writeln; }