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;

long test(ref string[] a, long[][] memo, long sx, long sy, long gx, long gy) {
    if(sx == gx && sy == gy) {
        return 0;
    }
    if(memo[sx][sy] != -1) {
        return memo[sx][sy];
    }
    auto dx = gx - sx;
    if(abs(dx) != 0) {
        dx /= abs(dx);
    }
    auto dy = gy - sy;
    if(abs(dy) != 0) {
        dy /= abs(dy);
    }
    long ans = 0;
    if(dx != 0 && a[sx+dx][sy] != '#') {
        ans = max(ans, test(a, memo, sx+dx, sy, gx, gy) + 1);
    }
    if(dy != 0 && a[sx][sy+dy] != '#') {
        ans = max(ans, test(a, memo, sx, sy+dy, gx, gy) + 1);
    }
    memo[sx][sy] = ans;
    return ans;
}

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;
            }
        }
    }
    long ans = long.max;
    foreach(i; iota(h)) {
        auto gx = i;
        auto gy = 0;
        auto memo = new long[][](h, w);
        foreach(j; iota(h)) {
            foreach(l; iota(w)) {
                memo[j][l] = -1;
            }
        }
        ans = min(ans, 1 + (abs(gx - sx) + abs(gy - sy) - min(k, test(a, memo, sx, sy, gx, gy)) + k-1) / k);
    }
    foreach(i; iota(h)) {
        auto gx = i;
        auto gy = w-1;
        auto memo = new long[][](h, w);
        foreach(j; iota(h)) {
            foreach(l; iota(w)) {
                memo[j][l] = -1;
            }
        }
        ans = min(ans, 1 + (abs(gx - sx) + abs(gy - sy) - min(k, test(a, memo, sx, sy, gx, gy)) + k-1) / k);
    }
    foreach(i; iota(w)) {
        auto gx = 0;
        auto gy = i;
        auto memo = new long[][](h, w);
        foreach(j; iota(h)) {
            foreach(l; iota(w)) {
                memo[j][l] = -1;
            }
        }
        ans = min(ans, 1 + (abs(gx - sx) + abs(gy - sy) - min(k, test(a, memo, sx, sy, gx, gy)) + k-1) / k);
    }
    foreach(i; iota(w)) {
        auto gx = h-1;
        auto gy = i;
        auto memo = new long[][](h, w);
        foreach(j; iota(h)) {
            foreach(l; iota(w)) {
                memo[j][l] = -1;
            }
        }
        ans = min(ans, 1 + (abs(gx - sx) + abs(gy - sy) - min(k, test(a, memo, sx, sy, gx, gy)) + k-1) / k);
    }
    writeln(ans);
}

