import sys from collections import deque # Function to check if driving is possible from start to finish def is_drivable(start, finish, grid): # Check if start and finish are not in the same hazard if grid[start[0]][start[1]] == '*' or grid[finish[0]][finish[1]] == '*': return False # Create a probability grid to keep track of probability of reaching each cell prob = [[0.0 for _ in range(C)] for _ in range(R)] prob[start[0]][start[1]] = 1.0 # Use dynamic programming to calculate probability of reaching each cell from start for _ in range(101): new_prob = [[0.0 for _ in range(C)] for _ in range(R)] for i in range(R): for j in range(C): if grid[i][j] == '#' or grid[i][j] == '*': continue directions = [('N', i-1, j), ('S', i+1, j), ('W', i, j-1), ('E', i, j+1)] for d, r, c in directions: if r < 0 or r >= R or c < 0 or c >= C or grid[r][c] == '#': r, c = i, j if d in ['N', 'S']: p = 0.5 if d == 'N' else 0.5 new_prob[r][c] += prob[i][j] * p else: p = 0.5 if d == 'W' else 0.5 new_prob[r][c] += prob[i][j] * p prob = new_prob if prob[finish[0]][finish[1]] >= 1 - 1e-100: return True return False # Read input and process test cases T = int(input().strip()) for t in range(1, T+1): R, C = map(int, input().strip().split()) grid = [input().strip() for _ in range(R)] starts = [] finishes = [] for i in range(R): for j in range(C): if grid[i][j].islower(): starts.append((i, j)) elif grid[i][j].isupper(): finishes.append((i, j)) drivable_pairs = [] for start in starts: for finish in finishes: if is_drivable(start, finish, grid): drivable_pairs.append(grid[start[0]][start[1]] + grid[finish[0]][finish[1]]) if drivable_pairs: print(f"Case #{t}: {' '.join(sorted(drivable_pairs))}") else: print(f"Case #{t}: NONE")