from math import ceil
from collections import deque


def start_pos():
    for r in range(H):
        for c in range(W):
            if a[r][c] == 'S':
                return r, c
    return -1, -1


def shortest_len(y, x):
    return min(y, H - 1 - y, x, W - 1 - x)


H, W, K = map(int, input().split())
a = [input() for _ in range(H)]
b = [[False for _ in range(W)] for _ in range(H)]
sy, sx = start_pos()
need_len = shortest_len(sy, sx)

q = deque()
q.append((sy, sx, 0))
while len(q) > 0:
    y, x, k = q.popleft()
    b[y][x] = True
    sl = shortest_len(y, x)
    need_len = min(need_len, sl)
    if sl - (K - k) >= need_len: continue
    if k == K: continue
    for dy, dx in (-1, 0), (1, 0), (0, -1), (0, 1):
        ny, nx = y + dy, x + dx
        if not(0 <= ny < H and 0 <= nx < W): continue
        if a[ny][nx] == '#': continue
        if b[ny][nx]: continue
        q.append((y + dy, x + dx, k + 1))

print(1 + ceil(need_len / K))
