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
ans=float("INF")
l=[[0]*w for i in range(h)]
p=[(x,y)]
def dfs(wx,wy,cnt):
    if cnt>k:return 1
    if l[wx][wy]:return 1
    for a,s in [(0,1),(0,-1),(1,0),(-1,0)]:
        if 0<=wx+a<h and 0<=wy+s<w and maze[wx+a][wy+s]==".":
            l[wx+a][wy+s]=1
            p.append((wx+a,s+wy))
            dfs(wx+a,wy+s,cnt+1)
            if (wx+a in (0,h-1)or wy+s in (0,w-1) )and cnt<k:
                global ans
                ans=1
                #print("!")
dfs(x,y,0)

for g,j in p:
    ans=min(ans,-((-min(g,w-j-1,h-g-1,j))//k)+1)
print(ans)