from collections import deque Directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] h, w = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(h)] visited = [[False for _ in range(w)] for _ in range(h)] for i in range(h): for j in range(w): if A[i][j] == 0: visited[i][j] = True ans = 0 for i in range(h): for j in range(w): if not visited[i][j]: ans += 1 Que = deque([(i, j)]) visited[i][j] = True while Que: ci, cj = Que.popleft() for di, dj in Directions: ni, nj = ci + di, cj + dj if 0 <= ni < h and 0 <= nj < w and not visited[ni][nj]: visited[ni][nj] = True Que.append((ni, nj)) print(ans)