// #define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return max(low, min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { t = clamp(t, low, high); }

namespace init__ {

struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

}

#ifdef DEBUGGING
#include "../debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

template <typename T>
T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
    return V<decltype(rec)>(s, rec);
#undef rec
}

ll H, W, K;
const ll inf = 5e15;

ll dist_to_outside(ll h, ll w) {
    return var_min(h, (H - 1) - h,
                   w, (W - 1) - w);
}

ll rest_magic(ll h, ll w) {
    ll dist = dist_to_outside(h, w);
    return dist / K + !!(dist % K);
}

int main() {
    cin >> H >> W >> K;
    V<string> A(H);
    for(auto &&s : A) cin >> s;
    ll sh, sw;
    for(ll i = 0; i < H; i++) for(ll j = 0; j < W; j++) if(A[i][j] == 'S') {
        sh = i;
        sw = j;
        break;
    }

    ll ans = inf;
    queue<pair<PLL, ll>> que;
    que.emplace(PLL(sh, sw), 0);
    VV<ll> dists = make_v<ll>(inf, H, W);
    dists[sh][sw] = 0;
    ll dh[] = { 1, 0, -1, 0 };
    ll dw[] = { 0, 1, 0, -1 };

    while(que.size()) {
        ll h, w, d;
        tie(h, w) = que.front().first;
        d = que.front().second;
        que.pop();
        if(dists[h][w] < d) continue;
        if(d == K) continue;
        chmin(ans, rest_magic(h, w));
        for(ll i = 0; i < 4; i++) {
            ll nh = h + dh[i];
            ll nw = w + dw[i];
            ll nd = d + 1;
            if(!(0 <= nh && nh < H &&
                 0 <= nw && nw < W)) continue;
            if(dists[nh][nw] <= nd) continue;
            if(A[nh][nw] != '.') continue;
            DEBUG(make_tuple(nh, nw));
            dists[nh][nw] = nd;
            que.emplace(PLL(nh, nw), nd);
        }
    }
    cout << ans + 1 << endl;
    return 0;
}

