H,W,K=map(int,input().split())
B=[]
S=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]=="S":
            S=[i,j]
            a[j]="."
    B.append(a)

A=[[10**5 for i in range(W)]for j in range(H)]

A[S[0]][S[1]]=0  
#print(A)
X=[1,-1,0,0]
Y=[0,0,1,-1]
Q=[[S[0],S[1]]]
for a in range(10**5):
    if len(Q)==0:
        break
    else:
        if (Q[0][0]-1)>=0:
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]-1][Q[0][1]] and B[Q[0][0]-1][Q[0][1]]==".":
                A[Q[0][0]-1][Q[0][1]]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0]-1,Q[0][1]])
        if (Q[0][0]+1)<=(H-1):
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]+1][Q[0][1]] and B[Q[0][0]+1][Q[0][1]]==".":
                A[Q[0][0]+1][Q[0][1]]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0]+1,Q[0][1]])
        if (Q[0][1]-1)>=0:
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]][Q[0][1]-1] and B[Q[0][0]][Q[0][1]-1]==".":
                A[Q[0][0]][Q[0][1]-1]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0],Q[0][1]-1])
        if (Q[0][1]+1)<=(W-1):
            if (A[Q[0][0]][Q[0][1]]+1)<A[Q[0][0]][Q[0][1]+1] and B[Q[0][0]][Q[0][1]+1]==".":
                A[Q[0][0]][Q[0][1]+1]=(A[Q[0][0]][Q[0][1]]+1)
                Q.append([Q[0][0],Q[0][1]+1])
    del Q[0]
                
#print(B)
#print(A)
#print(P)
ANS=10**5
for i in range(H):
    for j in range(W):
        if A[i][j]<=K:
            if ANS>min(i,j,(H-1-i),(W-1-j)):
                ANS=min(i,j,(H-1-i),(W-1-j))
                #print(ANS)
ans=ANS
#print(ANS)

if ans%K==0:
    print(ans//K+1)
else:
    print(ans//K+2)
