from collections import deque
from math import ceil
H,W,K=map(int,input().split())
grid=[input() for i in range(H)]

for i in range(H):
  for j in range(W):
    if grid[i][j]=="S":
      S=(i,j)

stack=deque([(S[0],S[1],0)])
visited=[[False]*(W) for i in range(H)]
visited[S[0]][S[1]]=True
a=float("inf")
t=[-1,1]
while stack:
  y,x,m=stack.popleft()
  if m>K:
    break
  a=min(a,min(x,W-1-x,y,H-1-y))
  if a==0:
    break
  for s1 in t:
    for s2 in t:
      if not visited[y+s1][x+s2] and grid[y+s1][x+s2]!="#":
        stack.append((y+s1,x+s2,m+1))
        visited[y+s1][x+s2]=True
ans=1+ceil(a/K)
print(ans)


    
        
        
          
  
