import sys
from collections import deque
input = sys.stdin.readline
inf = 10**8
inf = 10
DX = (-1, 0, 1, 0, -1, -1, 1, 1)
DY = (0, 1, 0, -1, -1, 1, -1, 1)
DX = DX[:4]
DY = DY[:4]


H, W, K = map(int, input().split())
grid = [input().rstrip() for _ in range(H)]


dist = [[inf]*W for _ in range(H)]
node = []
for i in range(H):
    for j in range(W):
        if grid[i][j] == "S":
            node.append((i, j))
            dist[i][j] = 1

# まず一回普通にBFS
d = 0
memo = []
while d < K:
    nxt = []
    while node:
        x, y = node.pop()
        if x in (0, H - 1) or y in (0, W - 1):
            print(dist[x][y])
            exit()
        memo.append((x, y))
        for dx, dy in zip(DX, DY):
            nx = x + dx
            ny = y + dy
            if nx < 0 or nx >= H or ny < 0 or ny >= W:
                continue
            if grid[nx][ny] == "#":
                continue
            if dist[nx][ny] >= inf:
                dist[nx][ny] = 1
                nxt.append((nx, ny))
    d += 1
    node = nxt
memo.extend(node)

ans = inf
for x, y in memo:
    d = min(x, y, H - x - 1, W - y - 1)
    ans = min(ans, 1 + (d + K - 1) // K)
print(ans)