import java.util.*; public class No697{ public static void main(String[] args){ run(); } static class DJSet{ int n; int[] upper; public DJSet(int n_) { n = n_; upper = new int[n]; Arrays.fill(upper, -1); } int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } boolean equiv(int x, int y) { return root(x) == root(y); } void setUnion(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (upper[x] > upper[y]) { x ^= y; y ^= x; x ^= y; } upper[x] += upper[y]; upper[y] = x; } } static void run(){ Scanner sc = new Scanner(System.in); int H = sc.nextInt(); int W = sc.nextInt(); DJSet ds = new DJSet(H * W); int[][] A = new int[H][W]; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { A[i][j] = sc.nextInt(); } } for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (A[i][j] != 1) continue; for (int di = -1; di <= 1; ++di) { for (int dj = -1; dj <= 1; ++dj) { if (Math.abs(di) + Math.abs(dj) != 1) continue; int ni = i + di; int nj = j + dj; if (ni < 0 || nj < 0 || ni >= H || nj >= W) continue; if (A[ni][nj] != 1) continue; ds.setUnion(i * W + j, ni * W + nj); } } } } int cnt = 0; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (A[i][j] == 1 && i * W + j == ds.root(i * W + j)) ++cnt; } } System.out.println(cnt); } }