import itertools, math, collections, sys
input = sys.stdin.readline
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
ans = min([i, H - 1 - i, j, W - 1 - j])
Q = collections.deque([[i, j, K, ans]])
while Q:
    h, w, res, dist = Q.popleft()
    A[h][w] = '#'
    ans = min([ans, h, H - 1 - h, w, W - 1 - w])
    if ans == 0:
        print(1)
        exit()
    if res:
        res -= 1
        if h and A[h - 1][w] == '.':
            Q += [[h - 1, w, res, ans]]
        if h < H - 1 and A[h + 1][w] == '.':
            Q += [[h + 1, w, res, ans]]
        if w and A[h][w - 1] == '.':
            Q += [[h, w - 1, res, ans]]
        if w < W - 1 and A[h][w + 1] == '.':
            Q += [[h, w + 1, res, ans]]
print(1 + math.ceil(ans / K))
