import sys input = sys.stdin.readline from collections import deque from itertools import product h, w = map(int, input().split()) a = [[0]*(w+2)] + [[0] + list(map(int, input().split())) + [0] for _ in range(h)] + [[0]*(w+2)] arrived = [[False]*(w+2) for _ in range(h+2)] dyx = ((1, 0), (-1, 0), (0, 1), (0, -1)) ans = 0 for i, j in product(range(1, h+1), range(1, w+1)): if a[i][j] == 0: continue if arrived[i][j]: continue ans += 1 q = deque([(i, j)]) while q: y, x = q.popleft() arrived[y][x] = True for dy, dx in dyx: new_y, new_x = (y+dy, x+dx) if a[new_y][new_x] == 0: continue if arrived[new_y][new_x]: continue q.append((new_y, new_x)) print(ans)