from collections import * H, W = map(int, input().split()) G = [] for i in range(H): G.append(list(map(int, input().split()))) seen = [0] * (H * W) ans = 0 Q = deque() dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] for i in range(H): for j in range(W): if not G[i][j]: continue if seen[i*W+j]: continue seen[i*W+j] = 1 ans += 1 Q.append(i*W+j) while Q: px, py = divmod(Q.popleft(), W) for k in range(4): x = px + dx[k] y = py + dy[k] if x < 0 or x > H - 1 or y < 0 or y > W - 1: continue if seen[x*W+y]: continue if not G[x][y]: continue seen[x*W+y] = 1 Q.append(x * W + y) print(ans)