import java.util.Scanner

import scala.collection.mutable

/**
  * Created by sonetsuyoshi on 2015/12/16.
  */

object Main extends App {
  val dirs = List[(Int, Int)]((0, 1), (0, -1), (1, 0), (-1, 0))

  val sc = new Scanner(System.in)
  val (h, w, k) = (sc.nextInt, sc.nextInt, sc.nextInt)
  val rooms = Array.ofDim[Char](h, w)
  for (i <- 0 until h) rooms(i) = sc.next().toCharArray

  val indexes = 0.until(h).flatMap(i => 0.until(w).map((i, _)))

  val (sx, sy) = indexes.find { case (i, j) => rooms(i)(j) == 'S' }.get
  val checked = mutable.HashSet[(Int, Int)]()

  def dfs(count: Int, x: Int, y: Int): Unit = {
    if (count > 0 &&
      0 <= x && x < w && 0 <= y && y < h &&
      rooms(y)(x) != '#' &&
      !checked.contains((x, y))) {
      checked.+=((x, y))
      for (dir <- dirs) {
        dfs(count - 1, x + dir._1, y + dir._2)
      }
    }
  }

  dfs(k, sx, sy)


  val ans = (checked.map{case (x, y) => List(x, w - 1 - x, y, h - 1 - y).min}.min + k - 1 ) / k + 1

  println(ans)
}
