H,W = map(int,input().split()) A = [list(map(int,input().split())) for _ in range(H)] memo = [[False] * W for _ in range(H)] ans = 0 q = [] for h in range(H): for w in range(W): if A[h][w] and memo[h][w] == False: ans += 1 memo[h][w] = True q.append((h,w)) while len(q): x,y = q.pop() for i,j in [[-1,0],[1,0],[0,1],[0,-1]]: if 0 <= x + i < H and 0 <= y + j < W: if A[x+i][y+j] and memo[x+i][y+j] == False: memo[x+i][y+j] = True q.append((x+i,y+j)) print(ans)