import copy
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)
#print(S)    

#print(B)
for i in range(H):
    for j in range(W):
        if (abs(S[0]-i)+abs(S[1]-j))<=K and B[i][j]==".":
            B[i][j]=1
        else:
            B[i][j]=2
#print(B)
P=[i for i in range(H*W)]
size=[1 for i in range(H*W)]

def find(x):
    if x==P[x]:
        return x
    else:
        return find(P[x])

def union(y,z):
    Y=find(y)
    Z=find(z)
    if size[Y]>=size[Z]:
        P[Z]=P[Y]
        size[Y]+=size[Z]
    else:
        P[Y]=P[Z]
        size[Z]+=size[Y]
        
for i in range(H):
    for j in range(W):
        if B[i][j]==1:
            if i-1>=0:
                if B[i-1][j]==1:
                    if find(i*W+j)!=find((i-1)*W+j):
                        union(i*W+j,(i-1)*W+j)
            if i+1<=(H-1):
                if B[i+1][j]==1:
                    if find(i*W+j)!=find((i+1)*W+j):
                        union(i*W+j,(i+1)*W+j)
            if j-1>=0:
                if B[i][j-1]==1:
                    if find(i*W+j)!=find((i)*W+(j-1)):
                        union(i*W+j,(i)*W+(j-1))
            if j+1<=(W-1):
                if B[i][j+1]==1:
                    if find(i*W+j)!=find((i)*W+(j+1)):
                        union(i*W+j,(i)*W+(j+1))
            
p=P[S[0]*W+S[1]]
#print(p)
#print(P)
ANS=[10**5]
for i in range(H):
    for j in range(W):
        if P[i*W+j]==p:
            ANS.append(min(i,j,(H-1-i),(W-1-j)))
ans=min(ANS)
#print(ANS)
if ans%K==0:
    print(ans//K+1)
else:
    print(ans//K+2)
