import std; struct Grid { int x; int y; } 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 move = [ Grid(-1, 0), Grid(0, 1), Grid(1, 0), Grid(0, -1) ]; int res; foreach (i; 0 .. H) { foreach (j; 0 .. W) { if (A[i][j] == 0) { continue; } ++res; Grid[] que; que ~= Grid(i, j); while (!que.empty) { auto f = que.front; que.popFront; A[f.x][f.y] = 0; foreach (m; move) { auto next = f; next.x += m.x, next.y += m.y; if (next.x < 0 || next.x >= H || next.y < 0 || next.y >= W) { continue; } if (A[next.x][next.y] == 0) { continue; } que ~= next; } } } } res.writeln; }