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

// template {{{
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define resz resize

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define sort_by(x, y) sort(all(x), [&](const auto& a, const auto& b) { return y; })

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;

using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpdd = vector<pdd>;
using vvpdd = vector<vpdd>;

template<typename T> void ckmin(T& a, const T& b) { a = min(a, b); }
template<typename T> void ckmax(T& a, const T& b) { a = max(a, b); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

namespace __algorithm {
    template<typename T> void dedup(vector<T>& v) {
        sort(all(v)); v.erase(unique(all(v)), v.end());
    }
    template<typename T> typename vector<T>::iterator find(vector<T>& v, const T& x) {
        auto it = lower_bound(all(v), x); return it != v.end() && *it == x ? it : v.end();
    }
    template<typename T> int index(vector<T>& v, const T& x) {
        auto it = find(v, x); assert(it != v.end() && *it == x); return int(it - v.begin());
    }
    template<typename C, typename T, typename OP> vector<T> prefixes(const C& v, T id, OP op) {
        vector<T> r(sz(v)+1, id); F0R (i, sz(v)) r[i+1] = op(r[i], v[i]); return r;
    }
    template<typename C, typename T, typename OP> vector<T> suffixes(const C& v, T id, OP op) {
        vector<T> r(sz(v)+1, id); F0Rd (i, sz(v)) r[i] = op(v[i], r[i+1]); return r;
    }
}
using namespace __algorithm;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
struct monostate {
    friend istream& operator>>(istream& is, const monostate& ms) { return is; }
    friend ostream& operator<<(ostream& os, const monostate& ms) { return os; }
    friend monostate operator+(const monostate& a, const monostate& b) { return a; }
} ms;
#pragma GCC diagnostic pop

namespace __io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(15);
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); }
    }
}
using namespace __io;
// }}}

int main() {
    setIO();
    
    int H, W, K;
    cin >> H >> W >> K;

    vs grid(H);
    for (int y=0;y<H;y++) {
        cin >> grid[y];
    }

    pii st;
    for (int y=0;y<H;y++) {
        for (int x=0;x<W;x++) {
            if (grid[y][x] == 'S') {
                st = {x, y};
            }
        }
    }

    queue<pii> bfs;
    vi distrow(W, -1);
    vvi dist(H, distrow);
    bfs.push(st);
    dist[st.second][st.first] = 0;

    vpii dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        for (const auto& dir : dirs) {
            int dx = dir.first;
            int dy = dir.second;

            int nx = cur.first + dx;
            int ny = cur.second + dy;

            if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;

            if (dist[ny][nx] == -1 && grid[ny][nx] != '#') {
                dist[ny][nx] = dist[cur.second][cur.first] + 1;
                bfs.push({nx, ny});
            }
        }
    }

    int best = W + H;
    for (int y=0;y<H;y++) {
        for (int x=0;x<W;x++) {
            if (dist[y][x] >= 0 && dist[y][x] <= K) {
                int cdist = min(min(x, W-1-x), min(y, H-1-y));
                // cout << x << " " << y << " " << cdist << endl;
                best = min(best, (cdist + K - 1) / K);
            }
        }
    }
    
    cout << best + 1 << endl;

    return 0;
}

Measured memory_bytes: 3456