# import sys; input = sys.stdin.buffer.readline # sys.setrecursionlimit(10**7) from collections import defaultdict from collections import deque con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) def BFS(H, W, L, visit, start): h, w = start Q = deque(); Q.append([h, w]) while Q: y, x = Q.popleft() if L[y - 1][x] == 1 and visit[y - 1][x] == 0: visit[y - 1][x] = 1 Q.append([y - 1, x]) if L[y + 1][x] == 1 and visit[y + 1][x] == 0: visit[y + 1][x] = 1 Q.append([y + 1, x]) if L[y][x - 1] == 1 and visit[y][x - 1] == 0: visit[y][x - 1] = 1 Q.append([y, x - 1]) if L[y][x + 1] == 1 and visit[y][x + 1] == 0: visit[y][x + 1] = 1 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)) visit = [[0] * (W + 2) for i in range(H + 2)] # print(L) cnt = 0 for i in range(1, H + 1): for j in range(1, W + 1): if visit[i][j] == 0 and L[i][j] == 1: cnt += 1 BFS(H + 2, W + 2, L, visit, [i, j]) print(cnt) if __name__ == '__main__': main()