import sys input = sys.stdin.readline from collections import deque N,D,H=map(int,input().split()) MAP=[] for i in range(N): S=input().strip() MAP.append(S) M=int(input()) T=[list(map(int,input().split())) for i in range(M)] for i in range(N): for j in range(N): if MAP[i][j]=="S": start=[i,j] elif MAP[i][j]=="G": goal=[i,j] elif MAP[i][j]=="K": key=[i,j] D=[[1<<30]*N for i in range(N)] U=[[""]*N for i in range(N)] x,y=start D[x][y]=0 Q=deque() Q.append((x,y)) while Q: x,y=Q.popleft() if x-1>=0 and MAP[x-1][y]!="#" and MAP[x-1][y]!="E" and D[x-1][y]>D[x][y]+1: D[x-1][y]=D[x][y]+1 Q.append((x-1,y)) U[x-1][y]="U" if x+1D[x][y]+1: D[x+1][y]=D[x][y]+1 Q.append((x+1,y)) U[x+1][y]="D" if y-1>=0 and MAP[x][y-1]!="#" and MAP[x][y-1]!="E" and D[x][y-1]>D[x][y]+1: D[x][y-1]=D[x][y]+1 Q.append((x,y-1)) U[x][y-1]="L" if y+1D[x][y]+1: D[x][y+1]=D[x][y]+1 Q.append((x,y+1)) U[x][y+1]="D" x,y=key ANS=[] while [x,y]!=start: #print(x,y) if U[x][y]=="D": ANS.append("D") x-=1 elif U[x][y]=="U": ANS.append("U") x+=1 elif U[x][y]=="R": ANS.append("R") y-=1 elif U[x][y]=="L": ANS.append("L") y+=1 D=[[1<<30]*N for i in range(N)] U=[[""]*N for i in range(N)] x,y=key D[x][y]=0 Q=deque() Q.append((x,y)) while Q: x,y=Q.popleft() if x-1>=0 and MAP[x-1][y]!="#" and MAP[x-1][y]!="E" and D[x-1][y]>D[x][y]+1: D[x-1][y]=D[x][y]+1 Q.append((x-1,y)) U[x-1][y]="U" if x+1D[x][y]+1: D[x+1][y]=D[x][y]+1 Q.append((x+1,y)) U[x+1][y]="D" if y-1>=0 and MAP[x][y-1]!="#" and MAP[x][y-1]!="E" and D[x][y-1]>D[x][y]+1: D[x][y-1]=D[x][y]+1 Q.append((x,y-1)) U[x][y-1]="L" if y+1D[x][y]+1: D[x][y+1]=D[x][y]+1 Q.append((x,y+1)) U[x][y+1]="D" x,y=goal ANS2=[] while [x,y]!=key: #print(x,y) if U[x][y]=="D": ANS2.append("D") x-=1 elif U[x][y]=="U": ANS2.append("U") x+=1 elif U[x][y]=="R": ANS2.append("R") y-=1 elif U[x][y]=="L": ANS2.append("L") y+=1 LANS=ANS[::-1]+ANS2[::-1] for com in LANS: print("M",com)