import sys
input = sys.stdin.readline
from collections import deque

h, w, k = map(int, input().split())
A = [input().rstrip() for _ in range(h)]
for i, a in enumerate(A):
  if "S" in a:
    sy = i
    sx = a.index("S")
    break

que = deque()
dist = [[-1]*w for _ in range(h)]
D = ((-1, 0), (0, -1), (1, 0), (0, 1))
        
def dfs(y, x):
  seen = [0]*h
  stack = [(y, x, 0)]
  while stack:
    ny, nx, cnt = stack.pop()
    dist[ny][nx] = k
    que.append((ny, nx))
    if cnt == k:
      continue
    for dy, dx in D:
      yy, xx = ny+dy, nx+dx
      if 0 <= yy < h and 0 <= xx < w:
        if A[yy][xx] == "#":
          continue
        if seen[yy] >> xx & 1:
          continue
        seen[yy] |= 1 << xx
        stack.append((yy, xx, cnt+1))

dfs(sy, sx)

while que:
  y, x = que.popleft()
  if y == 0 or y == h-1 or x == 0 or x == w-1:
    ans = -(-dist[y][x] // k)
    break
  for dy, dx in D:
    ny, nx = y+dy, x+dx
    if 0 <= ny < h and 0 <= nx < w:
      if dist[ny][nx] == -1:
        dist[ny][nx] = dist[y][x] + 1
        que.append((ny, nx))

print(ans)