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

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

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 mn = 1000
    for (p in v) {
        mn = min(arrayListOf(mn, p.first - 1, h - p.first, p.second - 1, w - p.second))
    }
    println((mn + K - 1) / K + 1)
}
