from collections import deque,defaultdict H,W,M=map(int,input().split()) S=[list(input()) for _ in range(H)] for h in range(H): for w in range(W): if S[h][w]=="S":s=(h,w) elif S[h][w]=="G":g=(h,w) vis=[[0 for _ in range(W)] for _ in range(H)] Q=deque([(s,0,0)]) while Q: (h,w),cnt,key=Q.popleft() vis[h][w]=1 if (h,w)==g: exit(print(cnt)) if "1"<=S[h][w]<="9":key=int(S[h][w]) for dh,dw in ((-1,0),(1,0),(0,-1),(0,1)): if not (0<=h+dh