#include <bits/stdc++.h>

using namespace std;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename Q_temp>
using smaller_queue = priority_queue <Q_temp, vector<Q_temp>, greater<Q_temp> >;

const int INF = (int) 1e9;
const ll LINF = (ll) 4e18;
const ll MOD = (ll) (1e9 + 7);
const double PI = acos(-1.0);
const int limit = 100010;

#define REP(i,m,n) for(ll i = m; i < (ll)(n); ++i)
#define rep(i,n) REP(i, 0, n)
#define MP make_pair
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define debug(x) cerr << #x << ":" << x << endl;
#define debug2(x) for(auto a : x) cerr << a << " "; cerr << endl;
#define debug3(x) rep(i, sizeof(x)) cerr << x[i] << " "; cerr << endl;

vector<pii> around = {MP(1, 0), MP(-1, 0), MP(0, 1), MP(0, -1)};

//------------------------------------------------------
const ll MAX_V = 810 * 810;

class dijkstra_pair {
private:

    struct edge {
        ll to;
        ll cost;
    };
    vector<edge> E[MAX_V];
    ll d[MAX_V];
    ll H, W;

public:

    dijkstra_pair(ll H_in, ll W_in) : H(H_in), W(W_in) {
    }

    void set_E(ll fi, ll fj, ll ti, ll tj, ll cost = 1) {
        E[fi * W + fj].push_back({ti * W + tj, cost});
    }

    ll get_d(ll vi, ll vj) {
        return d[vi * W + vj];
    }

    void calc(ll si, ll sj) {
        ll s = si * W + sj;
        priority_queue <pll, vector<pll>, greater<pll> > que;
        rep(i, MAX_V) d[i] = LINF; //INF
        que.emplace(0LL, s);
        d[s] = 0LL;
        while (!que.empty()) {
            pll p = que.top();
            que.pop();
            ll v = p.second;
            if (d[v] < p.first) continue;
            for (auto&& e : E[v]) {
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    que.emplace(d[e.to], e.to);
                }
            }
        }
    }
};

inline ll CEIL(ll a, ll b) {
    return (a + b - 1) / b;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll h, w, k;
    cin >> h >> w >> k;
    string a[h];
    rep(i, h) cin >> a[i];
    dijkstra_pair ds(h, w);

    rep(i, h) rep(j, w - 1) {
        if ((a[i][j] == '.' || a[i][j] == 'S')
                && (a[i][j + 1] == '.' || a[i][j + 1] == 'S')) {
            ds.set_E(i, j, i, j + 1);
            ds.set_E(i, j + 1, i, j);
        }
    }

    rep(i, h - 1) rep(j, w) {
        if ((a[i][j] == '.' || a[i][j] == 'S')
                && (a[i + 1][j] == '.' || a[i + 1][j] == 'S')) {
            ds.set_E(i, j, i + 1, j);
            ds.set_E(i + 1, j, i, j);
        }
    }
    ll si, sj;

    rep(i, h) rep(j, w) {
        if (a[i][j] == 'S') {
            si = i, sj = j;
            goto loopcut;
        }
    }
loopcut:

    ds.calc(si, sj);
    ll ans = LINF;

    rep(i, h) rep(j, w) {
        if (ds.get_d(i, j) <= k) {
            ll dist = CEIL(min({i, j, h - 1 - i, w - 1 - j}), k) + 1;
            ans = min(ans, dist);
        }
    }

    rep(i, h) rep(j, w) {
        if (i != 0 && i != h - 1 && j != 0 && j != w - 1) continue;
        if (ds.get_d(i, j) <= k) ans = 1;
    }

    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 71040