h,w,k=map(int,input().split())
a=[input() for _ in range(h)]
s=(0,0)
for i in range(h*w):
    x,y=divmod(i,w)
    if a[x][y]=='S':
        s=(x,y)
        break
from collections import deque
todo=deque([[0,s]])
seen=[[0]*w for _ in range(h)]
seen[s[0]][s[1]]=1
while todo:
    d,(x,y)=todo.popleft()
    if d==k:continue
    for dx,dy in ((0,1),(0,-1),(1,0),(-1,0)):
        if 0<=x+dx<h and 0<=y+dy<w and a[x+dx][y+dy]!='#' and seen[x+dx][y+dy]==0:
            todo.append((d+1,(x+dx,y+dy)))
            seen[x+dx][y+dy]=1
ans=h*w
for i in range(h*w):
    x,y=divmod(i,w)
    if seen[x][y]==1:
        tmp=min(x,y,h-x,w-y)
        tmp=(tmp+k-1)//k
        ans=min(ans,tmp)
print(ans+1)
