#include<bits/stdc++.h>
using namespace std;

int main(void) {
    long h, w, k;
    cin >> h >> w >> k;

    vector<string> a(h);
    for(auto &e: a) cin >> e;

    long si, sj;
    for(long i=0; i<h; i++)
        for(long j=0; j<w; j++)
            if(a[i][j] == 'S')
                si = i, sj = j;

    long dxy[] = {-1, 0, 1, 0, -1};
    set<pair<long,long>> s;
    {
        auto dfs = [&](auto dfs, pair<long,long> cur, long n) {
            long y, x;
            tie(y, x) = cur;

            if(n == 0) return;

            for(long di=0; di<4; di++) {
                long dx = dxy[di], dy = dxy[di+1];
                long nx = x + dx, ny = y + dy;
                if(!(0 <= nx && nx < w && 0 <= ny && ny <= h)) continue;
                if(s.count({ny, nx})) continue;
                if(a[ny][nx] == '#') continue;
                s.insert({ny, nx});
                dfs(dfs, {ny, nx}, n-1);
            }
        };

        s.insert({si, sj});
        dfs(dfs, {si, sj}, k);
    }

    auto f = [&](pair<long,long> t) {
        long y, x;
        tie(y, x) = t;
        return min(min(x, y), min(w-1-x, h-1-y));
    };

    long r = 1e9;
    for(auto &t: s) {
        long u = (f(t) + k-1) / k;
        r = min(r, u);
    }
    cout << 1+r << endl;
}
