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 INF = 1000000000
  val q: Queue<State> = LinkedList<State>()
  var dist = Array<IntArray>(h) { IntArray(w) { INF } }
  for (i in 0 until h) {
    for (j in 0 until w) {
      if (a[i][j] == 'S') {
        q.add(State(i, j))
        dist[i][j] = 0
      }
    }
  }
  val dx = arrayOf(1, 0, -1, 0)
  val dy = arrayOf(0, 1, 0, -1)
  var ans = INF
  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] <= dist[s.x][s.y] + 1) 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)
}
