
from math import ceil
H, W, K = map(int, input().split())
 
A = [None] * H
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 = [[False]*W for _ in range(H)]
visited[S[0]][S[1]]=True
next = [S]
for _ in range(K):
    next_t = []
    for p in next:
        x, y = p
        if is_edge(x, y):
            visited[x][y]=True
            continue
        if A[x - 1][y] == '.':
            if not visited[x-1][y]:
                next_t.append((x - 1, y))
        if A[x + 1][y] == '.':
            if not visited[x+1][y]:
                next_t.append((x + 1, y))
        if A[x][y - 1] == '.':
            if not visited[x][y-1]:
                next_t.append((x, y-1))
        if A[x][y + 1] == '.':
            if not visited[x][y+1]:
                next_t.append((x, y+1))
    next = next_t
 
 
def get_dist(x, y):
    return min(x, y, H - x - 1, W - y - 1)
 
min_dist = get_dist(*S)
for x in range(H):
    for y in range(W):
        if visited[x][y]:
            min_dist = min(min_dist, get_dist(*p))
 
print(ceil(min_dist / K) + 1)

