import java.util.Queue;
import java.util.ArrayDeque;

data class Point(val h:Int, val w:Int)

val dx = intArrayOf(-1, 1, 0, 0)
val dy = intArrayOf(0, 0, -1, 1)

fun main(args: Array<String>) {
    val (h, w, k) = readLine()!!.split(" ").map(String::toInt)
    var sh = 0
    var sw = 0
    var a = Array<String>(h, {""})
    for(i in 0 until h){
        a[i] = readLine()!!
        for(j in 0 until w){
            if(a[i][j].toString() == "S") {
                sh = i
                sw = j
            }
        }
    }
    var dist = Array(h, {Array(w, {1000})})
    var que = ArrayDeque<Point>()
    dist[sh][sw] = 0
    que.add(Point(sh, sw))
    while(que.isNotEmpty()){
       var top = que.poll()
       if(dist[top.h][top.w] < k){
           for(i in 0 until 4){
               val ch = top.h+dx[i]
               val cw = top.w+dy[i]
               if(ch >= 0 && ch < h && cw >= 0 && cw < w && a[ch][cw].toString() != "#"){
                   if(dist[ch][cw] > dist[top.h][top.w]+1){
                        dist[ch][cw] = dist[top.h][top.w] + 1
                        que.add(Point(ch, cw))
                   }
               }
           }
       } 
    }
    var ans = 1000
    Math.max(11, 10/2)
    for(i in 0 until h){
        for(j in 0 until w){
            if(dist[i][j] != 1000){
                ans = Math.min(ans, 1+(i+k-1)/k)
                ans = Math.min(ans, 1+(h-1-i+k-1)/k)
                ans = Math.min(ans, 1+(j+k-1)/k)
                ans = Math.min(ans, 1+(w-1-j+k-1)/k)
            }
        }
    }
    println(ans)
}