from collections import deque

H, W, K = map(int, raw_input().split())

MAP = [raw_input() for _ in xrange(H)]
visited = [[False for _ in xrange(W)] for _ in xrange(H)]

for y in range(H):
    x = MAP[y].find('S')
    if x != -1:
        sx = x
        sy = y

Q = deque()
Q.append((0, (sx, sy)))
visited[sy][sx] = True
while len(Q) > 0:
    cost, [x, y] = Q.popleft()
    for dx, dy in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
        px, py = x+dx, y+dy
        if 0 <= px < W and 0 <= py < H:
            if not visited[py][px] and MAP[py][px] != "#":
                visited[py][px] = True
                if cost + 1 < K:
                    Q.append((cost + 1, (px, py)))

d = 10000
for x in xrange(W):
    for y in xrange(H):
        if visited[y][x]:
            d = min(d, x, W-1-x)
            d = min(d, y, H-1-y)

print (d + K - 1) / K + 1
