H, W, K = map(int, input().split())
items = []

start = []
diff = float("inf")

for i in range(H):
    items.append(list(input()))
    for j in range(W):
        s = items[i][j]
        if s == "S":
            start = [i, j]
            diff = min(diff, min(i, H - 1 - i, min(j, W - 1 - j)))
            p = [i, j]

for i in range(H):
    for j in range(W):
        if abs(i - start[0]) + abs(j - start[1]) <= K and items[i][j] != "#":
            diff = min(diff, min(i, H - 1 - i, min(j, W - 1 - j)))

ans = 1
if diff == 0:
    print(ans)
else:
    print(ans + (K - 1 + diff) // K)
