import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

void readlnTo(T...)(ref T t) {
    auto s = readln().split();
    assert(s.length == t.length);
    foreach(ref ti; t) {
        ti = s[0].to!(typeof(ti));
        s = s[1..$];
    }
}

const real eps = 1e-10;

const long p = 1_000_000_000 + 7;

void test(ref string[] a, bool[][] memo, long x, long y, long h, long w, long k) {
    memo[x][y] = true;
    if(k == 0) {
        return;
    }
    auto dx = [1, 0, -1, 0];
    auto dy = [0, 1, 0, -1];
    foreach(i; iota(4)){ 
        auto nx = x + dx[i];
        auto ny = y + dy[i];
        if(nx < 0 || nx >= h || ny < 0 || ny >= w) {
            continue;
        }
        if(a[nx][ny] != '#' && !memo[nx][ny]) {
            test(a, memo, nx, ny, h, w, k-1);
        }
    }
}

void main(){
    long h, w, k;
    readlnTo(h, w, k);
    string[] a;
    foreach(i; iota(h)) {
        a ~= readln().chomp();
    }
    long sx, sy;
    foreach(i; iota(h)) {
        foreach(j; iota(w)) {
            if(a[i][j] == 'S') {
                sx = i;
                sy = j;
            }
        }
    }
    auto memo = new bool[][](h, w);
    test(a, memo, sx, sy, h, w, k);
    auto ans = long.max;
    foreach(i; iota(h)) {
        foreach(j; iota(w)) {
            if(!memo[i][j]) {
                continue;
            }
            ans = min(ans, (min(i, h-1-i, j, w-1-j) + k-1) / k + 1);
        }
    }
    writeln(ans);
}

