from sys import stdin
rr = lambda: stdin.readline().strip()
rrM = lambda: map(int, rr().split())

OPEN, CLOSE, START = '.#S'
R, C, K = rrM()
A = [rr() for _ in xrange(R)]

#Move up to K first.  Then, you can move freely.
for r, row in enumerate(A):
    for c, val in enumerate(row):
        if val == START: break
    if val == START: break

def neighbors(r, c):
    for cr, cc in ((r-1,c),(r+1,c),(r,c-1),(r,c+1)):
        if 0<=cr<R and 0<=cc<C and A[cr][cc] == OPEN:
            yield cr, cc

def dist(r, c):
    return min(r,c,R-1-r,C-1,c)

from collections import deque
start = (r, c)
seen = {start}
queue = deque([(start, 0)])
while queue:
    node, depth = queue.popleft()
    if depth == K: break
    for nei in neighbors(*node):
        if nei not in seen:
            seen.add(nei)
            queue.append((node, depth+1))

ans = 1e9
for r, c in seen:
    delta = dist(r, c)
    #delta of 0 is bns = 0
    #delta of 1...K is bns 1
    #delta of K+1..2K is bns 2
    if delta == 0:
        bns = 0
    else:
        bns = 1 + (delta - 1)/K
    ans = min(ans, 1 + bns)
print ans
