h, w = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(h)] def Find(x, par): if par[x] < 0: return x else: par[x] = Find(par[x], par) return par[x] def Unite(x, y, par, rank): x = Find(x, par) y = Find(y, par) if x != y: if rank[x] < rank[y]: par[y] += par[x] par[x] = y else: par[x] += par[y] par[y] = x if rank[x] == rank[y]: rank[x] += 1 def Same(x, y, par): return Find(x, par) == Find(y, par) def Size(x, par): return -par[Find(x, par)] par = [-1]*(h*w) rank = [0]*(h*w) for i, row in enumerate(A): for j, a in enumerate(row): v = i*w+j if a == 1: for di, dj in (-1, 0), (1, 0), (0, -1), (0, 1): ni, nj = i+di, j+dj if 0 <= ni < h and 0 <= nj < w: if A[ni][nj] == 1: u = ni*w+nj Unite(v, u, par, rank) S = set() for i, row in enumerate(A): for j, a in enumerate(row): v = i*w+j if a == 1: S.add(Find(v, par)) print(len(S))