import java.util.*

data class State(val x: Int, val y: 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))
    }
  }
  val dx = arrayOf(1, 0, -1, 0)
  val dy = arrayOf(0, 1, 0, -1)
  var dist = Array<IntArray>(h) { IntArray(w) { 0 } }
  var ans = 1000000000
  while (!q.isEmpty()) {
    val s = q.remove()
    if (s.x == 0 || s.x == h - 1 || s.y == 0 || s.y == w - 1) {
      ans = arrayOf(ans, (dist[s.x][s.y] + k - 1) / k).min()!!
      continue
    }
    for (i in 0 until 4) {
      val tx = s.x + dx[i]
      val ty = s.y + dy[i]
      if (dist[tx][ty] > 0) continue
      dist[tx][ty] = dist[s.x][s.y] + 1
      if (a[tx][ty] == '#') dist[tx][ty] = arrayOf(dist[tx][ty], k + 1).max()!!
      q.add(State(tx, ty))
    }
  }
  println(ans)
}
