from math import ceil
H, W, K = map(int, input().split())

A = [None] * H
S = None
for i in range(H):
    A[i] = input()
    #
    if 'S' in A[i]:
        for j in range(W):
            if A[i][j] == 'S':
                S = [i, j]
                A[i] = A[i].replace('S', '.')

def is_edge(x, y):
    return x == 0 or y == 0 or x == H - 1 or y == W - 1

def enum(x, y, m, k):
    if (x, y) in m or k == 0:
        return
    m.add((x, y))
    if is_edge(x, y):
        return
    if A[x - 1][y] == '.':
        enum(x - 1, y, m, k - 1)
    if A[x + 1][y] == '.':
        enum(x + 1, y, m, k - 1)
    if A[x][y - 1] == '.':
        enum(x, y - 1, m, k - 1)
    if A[x][y + 1] == '.':
        enum(x, y + 1, m, k - 1)

m = set()
enum(S[0], S[1], m, K)

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

min_dist = get_dist(S[0], S[1])
for p in m:
    min_dist = min(min_dist, get_dist(p[0], p[1]))

print(ceil(min_dist / K) + 1)
