import java.util.*
import java.util.Collections.min

val da = kotlin.arrayOf(-1, 0, 1, -1)

fun main(args: Array<String>) {
    val (h, w, K) = readLine()!!.split(" ").map { it.toInt() }

    val f = Array(h + 2) {CharArray(w + 2) {'#'}}

    for(i in 1..h) {
        f[i] = "#${readLine()!!}#".toCharArray()
    }

    val si = f.indexOfFirst { it.contains('S') }
    val sj = f[si].indexOf('S')

    f[si][sj] = ' '

    val v = ArrayList<Pair<Int, Int>>()
    val Q = ArrayDeque<Triple<Int, Int, Int>>()
    Q.addLast(Triple(si, sj, 0))
    v.add(Pair(si, sj))

    while(Q.isNotEmpty()) {
        val p = Q.first
        Q.pop()
        if (p.third == K) {
            continue
        }

        for(i in 0..3) {
            val np = Triple(p.first + da[i], p.second + da[3 - i], p.third + 1)
            if (f[np.first][np.second] != '.') {
                continue
            }
            f[np.first][np.second] = ' '
            v.add(Pair(np.first, np.second))
            Q.addLast(np)
        }
    }

    var res = 1000
    for (p in v) {
        fun ceil(n: Int, m: Int): Int = (n + m - 1) / m
        res = min(arrayListOf(
            res,
            ceil(p.first - 1, K),
            ceil(h - p.first, K),
            ceil(p.second - 1, K),
            ceil(w - p.second, K)
        ))
    }
    println(res + 1)
}
