import heapq
H, W, K = map(int, input().split())
A = ['' for _ in range(H)]
B = [[[H * W, 0, 0] for _ in range(W)] for _ in range(H)]
Q = []
for i in range(H):
  A[i] = input()
  for j in range(W):
    if A[i][j] == 'S':
      B[i][j] = [1, 0, 0]
      heapq.heappush(Q, [1, 0, 0, i, j])
def drill(x, y, a, b):
  if A[a][b] == '.':
    _B = list(B[x][y])
    _B[1] += 1
  else:
    _B = list(B[x][y])
    _B[2] += 1
    if _B[0] <= -(-_B[2] // K):
      _B[0] = -(-_B[2] // K) + 1
      _B[1] = 1
    else:
      _B[1] += 1
  if _B[1] > K:
    _B[1] - K
  if _B < B[a][b]:
    B[a][b] = _B
    heapq.heappush(Q, _B + [a, b])
while Q:
  d, w, b, x, y = heapq.heappop(Q)
  if x == 0 or x == W - 1 or y == 0 or y == H - 1:
    print(d)
    break
  drill(x, y, x - 1, y)
  drill(x, y, x, y - 1)
  drill(x, y, x + 1, y)
  drill(x, y, x, y + 1)