h,w = map(int,input().split()) a = [list(map(int,input().split())) for _ in range(h)] def dfs(i,j): a[i][j] = 0 q = [(i,j)] while q: i,j = q.pop() for ni,nj in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]: if 0 <= ni < h and 0 <= nj < w and a[ni][nj] == 1: a[ni][nj] = 0 q.append((ni,nj)) ans = 0 for i in range(h): for j in range(w): if a[i][j] == 1: ans += 1 dfs(i,j) print(ans)