from array import array from collections import deque H, W = map(int, input().split()) A = tuple(array('b', map(int, input().split())) for _ in range(H)) pair2int = lambda h, w: h * W + w int2pair = lambda i: divmod(i, W) nonvisited = array('b', [1] * (H * W)) directions = ((0, 1), (1, 0), (0, -1), (-1, 0)) ans = 0 # for i in range(H * W): # h, w = int2pair(i) # if A[h][w] and nonvisited[i]: # ans += 1 # # dfs # d = deque([i]) # while d: # x = d.pop() # h, w = int2pair(x) # nonvisited[x] = 0 # for dh, dw in directions: # hdh, wdw = h + dh, w + dw # if 0 <= hdh < H and 0 <= wdw < W and A[hdh][wdw] and nonvisited[pair2int(hdh, wdw)]: # d.append(pair2int(hdh, wdw)) print(ans)