import sys sys.setrecursionlimit(150000) def count_ponds(field): H = len(field) W = len(field[0]) visited = [[False for _ in range(W)] for _ in range(H)] def dfs(x, y): if x < 0 or x >= H or y < 0 or y >= W: return if field[x][y] == 0 or visited[x][y]: return visited[x][y] = True dfs(x+1, y) dfs(x-1, y) dfs(x, y+1) dfs(x, y-1) count = 0 for i in range(H): for j in range(W): if field[i][j] == 1 and not visited[i][j]: dfs(i, j) count += 1 return count field = [] H, W = map(int, input().split()) for _ in range(H): field.append(list(map(int, input().split()))) print(count_ponds(field))