from collections import deque
H,W,K,*L = open(0).read().split()
H,W,K = map(int, (H,W,K))
q = deque([])
step = [(-1,0),(1,0),(0,-1),(0,1)]
for i in range(H):
  flag = False
  for j in range(W):
    if L[i][j]=='S':
      q.append((i,j,0))
      flag = True
      break
  if flag:
    break
dist = [10**10 for i in range(H*W)]
ans = 10**10
while q:
  a,b,c = q.popleft()
  x = min(i,H-a-1,j,W-b-1)
  cnt = 1+(x+K-1)//K
  ans = min(cnt,ans)
  dist[a*W+b] = c
  if c+1>K:
    continue
  for dx,dy in step:
    n = a+dx
    m = b+dy
    if not(-1<n<H and -1<m<W):
      continue
    if dist[n*W+m]<c+1:
      continue
    if L[n][m]=='.':
      q.append((n,m,c+1))
print(ans)