import itertools, math
H, W, K = [int(_) for _ in input().split()]
A = [list(input()) for _ in range(H)]
for i, j in itertools.product(range(H), range(W)):
    if A[i][j] == 'S':
        break
stack = [[i, j, 0]]
cand = []
while stack:
    h, w, dist = stack.pop()
    print(h, w, dist)
    A[h][w] = '#'
    cand += [[h, w]]
    if dist < K:
        for nh, nw in [[h - 1, w], [h + 1, w], [h, w - 1], [h, w + 1]]:
            if 0 <= nh < H and 0 <= nw < W and A[nh][nw] == '.':
                stack += [[nh, nw, dist + 1]]
ans = float('inf')
for hw in cand:
    h, w = hw
    ans = min([
        ans,
        math.ceil(h / K),
        math.ceil((H - 1 - h) / K),
        math.ceil(w / K),
        math.ceil((W - 1 - w) / K)
    ])
print(ans + 1)
