from collections import deque H,W,M=map(int,input().split()) S=[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) def pack(h, w, cnt, key, k): return h*(k**3)+w*(k**2)+cnt*k+key def unpack(v, k): return v//(k**3), v%(k**3)//(k**2), v%(k**2)//k, v%k K=500 vis=set() Q=deque([pack(s[0],s[1],0,0,K)]) while Q: h,w,cnt,key=unpack(Q.popleft(),K) vis.add((h,w)) 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