H,W = map(int,input().split()) A = [list(map(int,input().split())) for _ in range(H)] count = 2 stack = [] for h in range(H): for w in range(W): if A[h][w] == 0:continue if A[h][w] == 1: A[h][w] = count count += 1 if h + 1 < H and A[h+1][w] == 1: A[h+1][w] = A[h][w] if w + 1 < W and A[h][w+1] == 1: A[h][w+1] = A[h][w] print(count-2)