def count_ponds(field): def dfs(x, y): stack = [(x, y)] while stack: cx, cy = stack.pop() for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: nx, ny = cx + dx, cy + dy if 0 <= nx < n and 0 <= ny < m and field[nx][ny] == 'W' and not visited[nx][ny]: visited[nx][ny] = True stack.append((nx, ny)) n = len(field) m = len(field[0]) visited = [[False] * m for _ in range(n)] pond_count = 0 for i in range(n): for j in range(m): if field[i][j] == 'W' and not visited[i][j]: visited[i][j] = True dfs(i, j) pond_count += 1 return pond_count # Example usage if __name__ == "__main__": field = [ "W........WW.", ".WWW.....WWW", "....WW...WW.", ".........WW.", ".........W..", "..W......W..", ".W.W.....WW.", "W.W.W.....W.", ".W.W......W.", "..W.......W." ] print(count_ponds(field))