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

visited = [tuple(S)]
l = 0
for _ in range(K):
    r = len(visited)
    for p in range(l, r):
        x, y = visited[p]
        if is_edge(x, y):
            continue
        if A[x - 1][y] == '.':
            if (x - 1, y) not in visited:
                visited.append((x - 1, y))
        if A[x + 1][y] == '.':
            if (x + 1, y) not in visited:
                visited.append((x + 1, y))
        if A[x][y - 1] == '.':
            if (x , y - 1) not in visited:
                visited.append((x, y - 1))
        if A[x][y + 1] == '.':
            if (x , y + 1) not in visited:
                visited.append((x, y + 1))
    l = r

 
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 visited:
    min_dist = min(min_dist, get_dist(*p))
 
print(ceil(min_dist / K) + 1)
