from collections import deque h, w = map(int, input().split()) grid = [input().split() for _ in range(h)] sensor = [] for j in range(h): for i in range(w): if grid[j][i] == '1': sensor.append((i, j)) visited = set() count = 0 while sensor: q = deque() q.append(sensor[0]) sensor.pop(0) while q: x, y = q.popleft() if (x, y) in visited: continue visited.add((x, y)) for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]: nx, ny = x + dx, y + dy if 0 <= nx < w and 0 <= ny < h: if grid[ny][nx] == '1' and (nx,ny) not in visited: q.append((nx, ny)) count += 1 print(count)