# import sys; input = sys.stdin.buffer.readline # sys.setrecursionlimit(10**7) # from collections import defaultdict from collections import deque def getlist(): return list(map(int, input().split())) def BFS(H, W, L, start): h, w = start Q = deque(); Q.append([h, w]) while Q: y, x = Q.popleft() if L[y - 1][x] == 1: L[y - 1][x] = 2 Q.append([y - 1, x]) if L[y + 1][x] == 1: L[y + 1][x] = 2 Q.append([y + 1, x]) if L[y][x - 1] == 1: L[y][x - 1] = 2 Q.append([y, x - 1]) if L[y][x + 1] == 1: L[y][x + 1] = 2 Q.append([y, x + 1]) #処理内容 def main(): H, W = getlist() L = [] L.append([0] * (W + 2)) for i in range(H): l = getlist() L.append([0] + l + [0]) L.append([0] * (W + 2)) cnt = 0 for i in range(1, H + 1): for j in range(1, W + 1): if L[i][j] == 1: cnt += 1 BFS(H + 2, W + 2, L, [i, j]) print(cnt) if __name__ == '__main__': main()