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
Q = collections.deque([[i, j, K]])
hmin = hmax = i
wmin = wmax = j
while Q:
    h, w, res = Q.popleft()
    A[h][w] = '#'
    hmin = min(hmin, h)
    hmax = max(hmax, h)
    wmin = min(wmin, w)
    wmax = max(wmax, w)
    if res:
        res -= 1
        if h and A[h - 1][w] == '.':
            Q += [[h - 1, w, res]]
        if h < H - 1 and A[h + 1][w] == '.':
            Q += [[h + 1, w, res]]
        if w and A[h][w - 1] == '.':
            Q += [[h, w - 1, res]]
        if w < W - 1 and A[h][w + 1] == '.':
            Q += [[h, w + 1, res]]
print(1 +
      min([math.ceil(_ / K)
           for _ in (hmin, H - 1 - hmax, wmin, W - 1 - wmax)]))
