directions = { "up": lambda row, col: (row - 1, col), "down": lambda row, col: (row + 1, col), "left": lambda row, col: (row, col - 1), "right": lambda row, col: (row, col + 1), } size = int(input()) matrix = [] bunny_row = 0 bunny_col = 0 for row in range(size): inner_list = input().split() matrix.append(inner_list) for col in range(size): if inner_list[col] == "B": bunny_row, bunny_col = row, col best_dir = "" best_path = [] best_sum = float("-inf") for direction, moving in directions.items(): current_row, current_col = bunny_row, bunny_col path = [] current_sum = 0 while True: current_row, current_col = moving(current_row, current_col) if current_row < 0 or current_col < 0 or current_row >= size or current_col >= size: break if matrix[current_row][current_col] == "X": break path.append([current_row, current_col]) current_sum += int(matrix[current_row][current_col]) if current_sum > best_sum and path: best_sum = current_sum best_dir = direction best_path = path print(best_dir) for path in best_path: print(path) print(best_sum)