h, w = map(int, input().split()) A = [] for _ in range(h): A += list(map(int, input().split())) directions = ((0, 1), (1, 0), (0, -1), (-1, 0)) ans = 0 for i in range(h): for j in range(w): if A[i * w + j] == 0: continue A[i * w + j] = 0 ans += 1 stack = [(i * w + j)] while stack: tmp = stack.pop() ii = tmp // w jj = tmp - ii * w for di, dj in directions: ni = ii + di nj = jj + dj if ni == -1 or ni == h or nj == -1 or nj == w: continue if A[ni * w + nj] == 0: continue A[ni * w + nj] = 0 stack.append(ni * w + nj) print(ans)