from collections import deque
from math import ceil
import sys
def input():
	return sys.stdin.readline()[:-1]

H, W, k = map(int, input().split())
m = [input() for _ in range(H)]
q = deque()
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

step = [[10**10 for _ in range(W)] for _ in range(H)]

for h in range(1, H-1):
	for w in range(1, W-1):
		if m[h][w] == "S":
			q.appendleft([h, w])
			sx, sy = h, w

step[sx][sy] = 0
while q:
	label = q.pop()
	if step[label[0]][label[1]] == k:
		continue
	for i in range(4):
		h, w = label[0] + dx[i], label[1] + dy[i]
		if 0 <= h < H and 0 <= w < W:
			if m[h][w] == "." and step[h][w] > step[label[0]][label[1]] + 1:
				q.appendleft([h, w])
				step[h][w] = step[label[0]][label[1]] + 1

min_dis = max(H, W)
for h in range(H):
	for w in range(W):
		if step[h][w] <= k:
			min_dis = min(min_dis, h, w, H-h-1, W-w-1)

print(1+ceil(min_dis/k))