import queue

INF = 1010101010

dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]

h, w, k = map(int, input().split())
a = [list(input()) for _ in range(h)]

sy = -1
sx = -1
for i in range(h):
    for j in range(w):
        if a[i][j] == 'S':
            sy = i
            sx = j

cost = [[INF for j in range(w)] for i in range(h)]
cost[sy][sx] = 0
que = queue.Queue()
que.put((sy, sx, 0))

while not que.empty():
    now = que.get()

    if cost[now[0]][now[1]] < now[2]:
        continue

    for i in range(4):
        ny = now[0] + dy[i]
        nx = now[1] + dx[i]
        nc = now[2] + 1

        if 0 <= ny < h and 0 <= nx < w and a[ny][nx] != '#'and nc < cost[ny][nx]:
            cost[ny][nx] = nc
            que.put((ny, nx, nc))

ans = INF
for i in range(h):
    for j in range(w):
        if cost[i][j] <= k:
            ans = min(ans, 1 + (min(i, h - 1 - i, j, w - 1 - j) + k - 1) // k)

print(ans)