from collections import deque H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] dx = [0, 1, 0, -1] dy = [-1, 0, 1, 0] ans = 0 for h in range(H): for w in range(W): if A[h][w] == 1: ans += 1 que = deque() que.append((h, w)) A[h][w] = 0 while que: y, x = que.popleft() for i in range(4): xx = x + dx[i] yy = y + dy[i] if 0 <= xx < W and 0 <= yy < H: if A[yy][xx] == 1: que.append((yy, xx)) A[yy][xx] = 0 print(ans)