import sys
sys.setrecursionlimit(500000) #これがないと再帰の回数オーバーでエラー出る。

H,W,K = map(int,input().split())
Room = []
for i in range(H):
  temp = str(input())
  temp = list(temp)
  for j in range(W):
    if temp[j] == "S":
      sx = i;sy =j
  Room.append(temp)
#print(sx,sy)
d = [(1,0),(-1,0),(0,1),(0,-1)]
L = set([])
reach = set([])
reach.add((sx,sy))
def dfs(x,y,stp):
  if x==0 or x==H-1 or y==0 or y==W-1:
    print(1)
    exit()
  if Room[x][y] == "#":
    return
  if stp <= K:
    L.add((x,y))
    if stp == K:
      return
  stp +=1
  for i in range(4):
    nx = x+d[i][0]
    ny = y+d[i][1]
    if nx<0 or nx>=H or ny<0 or ny>= W or Room[nx][ny] == "#" or (nx,ny) in reach:
      continue
    reach.add((nx,ny))
    dfs(nx,ny,stp)

dfs(sx,sy,0)
INF = float("inf")
ans = INF

for t in L:
  x,y = t[0],t[1]
  nokori = min(x,H-1-x,y,W-1-y)
  step = (nokori+K-1)//K + 1 #1は最初のステップ
  ans = min(ans,step)
print(ans)
        
        
        
              