from collections import deque def bfs(sx, sy): S[sx][sy] = 0 queue = deque([]) queue.append((sx, sy)) while len(queue) > 0: cx, cy = queue.popleft() for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: nx, ny = cx + dx, cy + dy if 0 <= nx < H and 0 <= ny < W: if S[nx][ny] == 1: S[nx][ny] = 0 queue.append((nx, ny)) H, W = map(int, input().split()) S = [list(map(int, input().split())) for _ in range(H)] ans = 0 for i in range(H): for j in range(W): if S[i][j] == 1: bfs(i, j) ans += 1 print(ans)