class No697: height, width = 0, 0 id = 2 def __init__(self): self.height, self.width = map(int, input().split(" ")) self.water_map = [[0 for i in range(self.width)] for j in range(self.height)] for i in range(self.height): self.water_map[i] = list(map(int, input().split(" "))) def solve(self): search_dic = [(0, 1, 0, -1), (1, 0, -1, 0)] for i in range(self.height): for j in range(self.width): if self.water_map[i][j] == 1: pos_id = self.id for k in range(4): vx = j + search_dic[0][k] vy = i + search_dic[1][k] if 0 <= vx < self.width and 0 <= vy < self.height: if not self.water_map[vy][vx] == 0 and not self.water_map[vy][vx] == 1: pos_id = self.water_map[vy][vx] self.water_map[i][j] = pos_id if pos_id == self.id: self.id += 1 for i in range(self.height): for j in range(self.width): my_id = self.water_map[i][j] c_id = my_id if not my_id == 0: for k in range(4): vx = j + search_dic[0][k] vy = i + search_dic[1][k] if 0 <= vx < self.width and 0 <= vy < self.height: if not self.water_map[vy][vx] == 0 and not self.water_map[vy][vx] == my_id: c_id = self.water_map[vy][vx] self.water_map[i][j] = c_id num_set = set() for num_list in self.water_map: num_set = num_set | set(num_list) if num_set.isdisjoint({0}): return len(num_set) else: return len(num_set) - 1 if __name__ == "__main__": que = No697() ans = que.solve() print(ans)