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