import java.util.*

fun update(record: Int, challenger: Int): Int {
    if (0 > record || record > challenger) {
        return challenger
    }
    return record
}
fun main(args: Array<String>){
	val cin = Scanner(System.`in`)
    val h = cin.nextInt()
    val w = cin.nextInt()
    val k = cin.nextInt()
    cin.nextLine()

    val a = Array<Array<Int>>(h, {Array<Int>(w, {0})})
    var start: Array<Int> = arrayOf()
    for (i in 0..h-1) {
        val s = cin.nextLine()
        for (j in 0..w-1) {
            val c = s[j]
            if (c == '.') {
                a[i][j] = 0
            } else if (c == '#') {
                a[i][j] = -1
            }  else {
                a[i][j] = 1
                start = arrayOf(i, j)
            }
        }
    }

    val distance = Array<Array<Int>>(h, {Array<Int>(w, {-1})})
    distance[start[0]][start[1]] = 0
    val queue: MutableList<Array<Int>> = mutableListOf(start)

    while (queue.size != 0) {
        val now = queue.removeAt(0)
        val now_d = distance[now[0]][now[1]]
        if (k > now_d) {
            if (now[1] > 0 && a[now[0]][now[1]-1] == 0 && distance[now[0]][now[1]-1] == -1) {
                distance[now[0]][now[1]-1] = now_d + 1
                queue.add(arrayOf(now[0], now[1]-1))
            }
            if (now[0] > 0 && a[now[0]-1][now[1]] == 0 && distance[now[0]-1][now[1]] == -1) {
                distance[now[0]-1][now[1]] = now_d + 1
                queue.add(arrayOf(now[0]-1, now[1]))
            }
            if (w-1 > now[1] && a[now[0]][now[1]+1] == 0 && distance[now[0]][now[1]+1] == -1) {
                distance[now[0]][now[1]+1] = now_d + 1
                queue.add(arrayOf(now[0], now[1]+1))
            }
            if (h-1 > now[0] && a[now[0]+1][now[1]] == 0 && distance[now[0]+1][now[1]] == -1) {
                distance[now[0]+1][now[1]] = now_d + 1
                queue.add(arrayOf(now[0]+1, now[1]))
            }
        }
    }

    var record = -1
    for (i in 0..h-1) {
        for (j in 0..w-1) {
            if (distance[i][j] >= 0) {
                record = update(record, i)
                record = update(record, j)
                record = update(record, h-1-i)
                record = update(record, w-1-j)
            }
        }
    }

    println((record+k-1)/k + 1)
}