h,w,k=map(int,input().split())
maze=[input()for i in range(h)]
for i in range(h):
    for j in range(w):
        if maze[i][j]=="S":x,y=i,j;break
    else:
        continue
    break
l=[[700000,700000]for i in range(h*w)]
l[w*x+y]=[0,0]
from collections import deque
dq=deque([(x,y)])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
while len(dq):
    a,b=dq.popleft()
    for c,d in [(0,1),(0,-1),(1,0),(-1,0)]:
        if 0<=a+c<h and 0<=b+d<w:
            op,cl=l[w*a+b]
            if maze[a+c][b+d]=="#":cl+=1
            else:op+=1
            if -(-l[(a+c)*w+b+d][0]//k)-(-l[(a+c)*w+b+d][1]//k)>=-(-op//k)-(-cl//k):
                l[(a+c)*w+b+d]=[op,cl]
                dq.append((a+c,b+d))
ans=min(min((s+k)//k+(d+k-1)//k for s,d in l[0:w]),min((s+k)//k+(d+k-1)//k for s,d in l[-w:]),min((s+k)//k+(d+k-1)//k for s,d in l[0::w]),min((s+k)//k+(d+k-1)//k for s,d in l[w-1::w]))
print(ans)
