import strutils, sequtils

let get = iterator: string {.closure.} =
  for s in stdin.readAll.split: yield s
proc read: int = get().parseInt

###############################################################################

import queues
const big = 10000000

let h, w, k = read()
let a = newSeqWith(h, get())

type point = tuple[y, x: int]
var que = initQueue[point]()
var dis = newSeqWith(h, newSeqWith(w, big))

for i in 0..<h:
  for j in 0..<w:
    if a[i][j] == 'S':
      dis[i][j] = 0
      que.add((i, j))
  
while que.len > 0:
  let (y, x) = que.dequeue
  if y == 0 or y == h-1 or x == 0 or x == w-1:
    echo "1"
    quit 0
  for p in [(y: 0, x: 1), (1, 0), (-1, 0), (0, -1)]:
    let (ny, nx) = (y+p.y, x+p.x)
    if a[ny][nx] != '#' and dis[ny][nx] == big:
      dis[ny][nx] = dis[y][x] + 1
      que.add((ny, nx))

var sts = newSeq[point]()
      
for i in 0..<h:
  for j in 0..<w:
    if dis[i][j] <= k:
      sts.add((i, j))

var ans = big

for p in sts:
  ans = [ans, p.y, p.x, h-1-p.y, w-1-p.x].min()

echo 1 + (ans + k - 1) div k