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

MAP = [raw_input() for _ in range(H)]

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

Q = [(sx, sy)]
V = set()
while len(Q) > 0:
    x, y = Q.pop()
    V.add((x, y))
    for dx, dy in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
        px, py = x+dx, y+dy
        if px < 0 or px >= W:
            continue
        if py < 0 or py >= H:
            continue
        if (px, py) in V:
            continue
        if MAP[py][px] == "#":
            continue
        Q.append((px, py))

d = 10000
for x, y in V:
    d = min(d, x, W-1-x)
    d = min(d, y, H-1-y)

if d == 0:
    print 1
elif d < K:
    print 2
else:
    print 1 + d/K
