import heapq
import sys
input=sys.stdin.readline
H,W,K=map(int,input().split())

s=[0,0]
A=[]
for i in range(H):
    a=list(input())
    for j in range(W):
        if a[j]=="S":
            s=[i,j]
    A.append(a)
#print(A)
#print(s)
B=[[10**6 for j in range(W)]for i in range(H)]
#print(B)
B[s[0]][s[1]]=0
Q=[]
for i in range(4):
    if s[0]+1>=H:
        print(1)
        exit()
    elif A[s[0]+1][s[1]]==".":
        Q.append([1,s[0]+1,s[1]])
    if s[0]-1<0:
        print(1)
        exit()
    elif A[s[0]-1][s[1]]==".":
        Q.append([1,s[0]-1,s[1]])
    if s[1]+1>=W:
        print(1)
        exit()
    elif A[s[0]][s[1]+1]==".":
        Q.append([1,s[0],s[1]+1])
    if s[1]-1<0:
        print(1)
        exit()
    elif A[s[0]][s[1]-1]==".":
        Q.append([1,s[0],s[1]-1])
        
heapq.heapify(Q)
for i in range(10**6):
    if len(Q)==0:
        break
    else:
        if B[Q[0][1]][Q[0][2]]==10**6:
            B[Q[0][1]][Q[0][2]]=Q[0][0]
            q=Q[0]
            heapq.heappop(Q)
            for j in range(4):
                if q[1]+1<=H-1 and K>=q[0]:
                    if A[q[1]+1][q[2]]==".":
                        heapq.heappush(Q,[q[0]+1,q[1]+1,q[2]])
                if q[1]-1>=0 and K>=q[0]:
                    if A[q[1]-1][q[2]]==".":
                        heapq.heappush(Q,[q[0]+1,q[1]-1,q[2]])
                if q[2]+1<=W-1 and K>=q[0]:
                    if A[q[1]][q[2]+1]==".":
                        heapq.heappush(Q,[q[0]+1,q[1],q[2]+1])
                if q[2]-1>=0 and K>=q[0]:
                    if A[q[1]][q[2]-1]==".":
                        heapq.heappush(Q,[q[0]+1,q[1],q[2]-1])
    heapq.heappop(Q)
#print(A)
#print(B)

import math

MIN=10**6
for i in range(H):
    for j in range(W):
        if B[i][j]<=K:
            U=math.ceil(i/K)
            L=math.ceil(j/K)
            D=math.ceil((H-1-i)/K)
            R=math.ceil((W-1-j)/K)
            if min(U,L,D,R)+1<MIN:
                MIN=min(U,L,D,R)+1
print(MIN)
