from collections import deque from itertools import combinations, product def main(): H, W = map(int, input().split()) field_map = tuple(tuple(map(int, input().split())) for _ in range(H)) searched = [[0]*W for _ in range(H)] pond_ctr = 0 for height_idx, width_idx in product(range(H), range(W)): if field_map[height_idx][width_idx] == 0: continue if searched[height_idx][width_idx] == 1: continue pond_ctr += 1 searching = deque(((height_idx, width_idx),)) searched[height_idx][width_idx] = 1 while searching: current_h_idx, current_w_idx = searching.popleft() if field_map[current_h_idx][current_w_idx] == 0: continue if current_h_idx < H-1 and searched[current_h_idx+1][current_w_idx] == 0: searching.append((current_h_idx+1, current_w_idx)) searched[current_h_idx+1][current_w_idx] = 1 if current_h_idx > 0 and searched[current_h_idx-1][current_w_idx] == 0: searching.append((current_h_idx-1, current_w_idx)) searched[current_h_idx-1][current_w_idx] = 1 if current_w_idx < W-1 and searched[current_h_idx][current_w_idx+1] == 0: searching.append((current_h_idx, current_w_idx+1)) searched[current_h_idx][current_w_idx+1] = 1 if current_w_idx > 0 and searched[current_h_idx][current_w_idx-1] == 0: searching.append((current_h_idx, current_w_idx-1)) searched[current_h_idx][current_w_idx-1] = 1 print(pond_ctr) if __name__ == "__main__": main()