import itertools if __name__ == '__main__': h, w = map(int, input().split()) matrix = [list(map(int, input().split())) for _ in range(h)] diffs = ((1, 0), (0, 1)) counter = 0 for x, y in itertools.product(range(h), range(w)): if matrix[x][y] == 0: continue counter += 1 stack = [(x, y)] while stack: i, j = stack.pop() if not(0 <= i < h and 0 <= j < w and matrix[i][j] == 1): continue matrix[i][j] = 0 for di, dj in diffs: ni, nj = i + di, j + dj if 0 <= ni < h and 0 <= nj < w: stack.append((ni, nj)) print(counter)