import std.experimental.all; T read(T)() { return readln.chomp.to!T; } T[] reads(T)() { return readln.split.to!(T[]); } alias readint = read!int; alias readints = reads!int; int calc(int[][] g) { int rows = cast(int)g.length; int cols = cast(int)g[0].length; auto ds = [[-1, 0], [1, 0], [0, -1], [0, 1]]; void fill(int row, int col, int[][] g, bool[][] used) { auto q = DList!(int)(); // q.insertBack([row, col]); q.insertBack((row << 16) | col); while (!q.empty) { auto a = q.front; q.removeFront(); // int r = a[0], c = a[1]; int r = (a >> 16), c = a & 0xffff; if (!(0 <= r && r < rows && 0 <= c && c < cols)) continue; if (g[r][c] == 0) continue; if (used[r][c]) continue; used[r][c] = true; foreach (d; ds) { int r2 = r + d[0]; int c2 = c + d[1]; if (!(0 <= r2 && r2 < rows && 0 <= c2 && c2 < cols)) continue; if (g[r2][c2] == 0 || used[r2][c2]) continue; // q.insertBack([r2, c2]); q.insertBack((r2 << 16) | c2); } } } int ans = 0; auto used = new bool[][](rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (used[i][j]) continue; if (g[i][j] == 1) { ans++; fill(i, j, g, used); } } } return ans; } void main() { auto hw = readints; int h = hw[0], w = hw[1]; auto g = new int[][](h, w); for (int i = 0; i < h; i++) { g[i][] = readints; } writeln(calc(g)); }