import strutils
import sequtils
import algorithm
import math
import queues
import tables
import sets
import logging
import future

const INF* = int(1e18 + 373)

when not defined(release):
  addHandler(newFileLogger(stderr, lvlAll, "[ $levelname ] "))

proc readInt1*(): int = stdin.readLine().strip().parseInt()
proc readInt2*(): (int, int) =
    let v = stdin.readLine().strip().split().map(parseInt)
    return (v[0], v[1])
proc readInt3*(): (int, int, int) =
    let v = stdin.readLine().strip().split().map(parseInt)
    return (v[0], v[1], v[2])
proc readInt4*(): (int, int, int, int) =
    let v = stdin.readLine().strip().split().map(parseInt)
    return (v[0], v[1], v[2], v[3])

proc readSeq*(): seq[string] = stdin.readLine().strip().split()
proc readSeq*(n: Natural): seq[string] =
  result = newSeq[string](n)
  for i in 0..<n:
    result[i] = stdin.readLine().strip()

type seq2*[T] = seq[seq[T]]
proc newSeq2*[T](n1, n2: Natural): seq2[T] = newSeqWith(n1, newSeq[T](n2))

#------------------------------------------------------------------------------#
proc dfs(h, w: int; a: var seq2[char]; y, x: int) =
  a[y][x] = 'S'
  if y + 1 in 0..<h and x in 0..<w and a[y + 1][x] == '.':
    dfs(h, w, a, y + 1, x)
  if y - 1 in 0..<h and x in 0..<w and a[y - 1][x] == '.':
    dfs(h, w, a, y - 1, x)
  if y in 0..<h and x + 1 in 0..<w and a[y][x + 1] == '.':
    dfs(h, w, a, y, x + 1)
  if y in 0..<h and x - 1 in 0..<w and a[y][x - 1] == '.':
    dfs(h, w, a, y, x - 1)

proc main() =
  let (h, w, k) = readInt3()

  var a = newSeq2[char](h, 0)
  for i in 0..<h:
    a[i] = stdin.readLine().map(it => it)

  var sy = -1
  var sx = -1
  block search_takahashikun:
    for y in 0..<h:
      for x in 0..<w:
        if a[y][x] == 'S':
          sy = y
          sx = x
          a[sy][sx] = '.'
          break search_takahashikun

  dfs(h, w, a, sy, sx)

  var minD = INF
  for y in 0..<h:
    for x in 0..<w:
      if a[y][x] != 'S':
        continue
      let d = min(@[ y, abs(y - (h - 1)), x, abs(x - (w - 1))])
      minD = min(minD, d)

  let ans = 1 + (minD + k - 1) div k
  echo ans

main()

