import sys input = sys.stdin.readline from collections import deque H,W = map(int,input().split()) A = [list(map(int,input().split())) for y in range(H)] ans = 0 #dxy = [(-1,0), (0,-1), (0,1), (1,0)] dxy = [(0,1), (1,0)] for i in range(H) : for j in range(W) : if A[i][j] != 1 : continue ans += 1 A[i][j] = 0 q = deque([(i, j)]) while q : y, x = q.popleft() for dy,dx in dxy : ny,nx = y+dy, x+dx if ny<0 or ny>=H or nx<0 or nx>=W : continue if A[ny][nx] != 1 : continue q.append((ny, nx)) A[ny][nx] = 0 print(ans)