from collections import deque H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] def bfs(sr, sc, A, H, W): # 幅優先探索 que = deque((sr, sc)) while que: r, c = que.popleft() for dr, dc in zip([1, -1, 0, 0], [0, 0, 1, -1]): rr, cc = r + dr, c + dc if 0 <= rr < H and 0 <= cc < W and A[rr][cc]: que.append((rr, cc)) # 水を抜く A[rr][cc] = 0 ans = 0 for i in range(H): for j in range(W): if A[i][j]: ans += 1 bfs(i, j, A, H, W) print(ans)