import java.util.*

data class State(val x: Int, val y: Int, val d: Int, val c: Int)

fun main(args: Array<String>) {
  val (h, w, k) = readLine()!!.split(" ").map { it.toInt() }
  val a = Array<String>(h) { readLine()!! }
  val q: Queue<State> = LinkedList<State>()
  for (i in 0 until h) {
    for (j in 0 until w) {
      if (a[i][j] == 'S') q.add(State(i, j, 0, 0))
    }
  }
  val dx = arrayOf(1, 0, -1, 0)
  val dy = arrayOf(0, 1, 0, -1)
  var used = Array<BooleanArray>(h) { BooleanArray(w) { false } }
  var ans = 10000000
  while (!q.isEmpty()) {
    val s = q.remove()
    if (used[s.x][s.y]) continue
    used[s.x][s.y] = true
    for (i in 0 until 4) {
      val tx = s.x + dx[i]
      val ty = s.y + dy[i]
      if (tx < 0 || h - 1 < tx || ty < 0 || w - 1 < ty) {
        ans = arrayOf(ans, (s.d + k - 1) / k).min()!!
        continue
      }
      var d = s.d + 1
      var c = s.c
      if (a[tx][ty] == '#') {
        c++
        d = arrayOf(d, c + k).max()!!
      }
      q.add(State(tx, ty, d, c))
    }
  }
  println(ans)
}
