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