#include <bits/stdc++.h>

using namespace std;

#define sqr(x) ((x) * (x))
#define pb push_back
#define mp make_pair
#define ins insert
#define f first
#define s second
#define fin(name) freopen(name, "r", stdin)
#define fout(name) freopen(name, "w", stdout)
#define files(name) fin(name".in"); fout(name".out")
#define endl "\n"
#define fi(st,n) for (int i = (st); i <= (int)(n); ++i)
#define fj(st,n) for (int j = (st); j <= (int)(n); ++j)
#define fk(st,n) for (int k = (st); k <= (int)(n); ++k)
#define fq(st,n) for (int q = (st); q <= (int)(n); ++q)
#define fw(st,n) for (int w = (st); w <= (int)(n); ++w)
#define ff(i, st, n) for (int (i) = (st); (i) <= (int)(n); ++(i))
#define ei(st,n) for (int i = (st); i >= (int)(n); --i)
#define ej(st,n) for (int j = (st); j >= (int)(n); --j)
#define ek(st,n) for (int k = (st); k >= (int)(n); --k)
#define ef(i, st, n) for (int (i) = (st); (i) >= (int)(n); --(i))
#define ri(st,n) for (int i = (st); i < (int)(n); ++i)
#define rj(st,n) for (int j = (st); j < (int)(n); ++j)
#define rk(st,n) for (int k = (st); k < (int)(n); ++k)
#define rq(st,n) for (int q = (st); q < (int)(n); ++q)
#define rf(i, st, n) for (int (i) = (st); (i) < (int)(n); ++(i))
#define clean(a) memset((a),0,sizeof (a))
#define sync ios_base::sync_with_stdio(0);cin.tie(0)
#define y1 dsklmlvmd

typedef long long ll;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ldbl;
typedef pair <int, int> pii;

const int inf = (int)1e9;
const ll linf = (ll)1e18;
const dbl eps = (dbl) 1e-8;
const int mod = (int) 1e9 + 7;
const int maxn = (int) 1e5 + 5;
//const dbl M_PI = (dbl)2 * (dbl)acos(0);

//cout<<fixed<<setprecision(10);
//srand(time(0));

int k;
set <pair <int, pii> > q;
int a[888][888], d[888][888];
pii st;
int n, m;


int main() {
//    fin("t.in");
    sync;
    cin >> n >> m >> k;
    fi(1, n) {
        string s;
        cin >> s;
        fj(1, m) {
            if (s[j - 1] == '#')
                a[i][j] = 1;
            else
                a[i][j] = 0;
            if (s[j - 1] == 'S') {
                st = mp(i, j);
            }
        }
    }
    memset(d, -1, sizeof d);
    fi(0, n + 1) {
        fj(0, m + 1) {
            if (i == 0 || i == n + 1 || j == 0 || j == m + 1) {
                d[i][j] = 0;
            }
        }
    }
    q.clear();
    d[st.f][st.s] = 0;
    q.ins(mp(0, st));
    while (q.size()) {
        pii pos;
        int s;
        s = q.begin()->f;
        pos = q.begin()->s;
        q.erase(q.begin());
        fi(-1, 1) {
            fj(-1, 1) {
                if (abs(i) + abs(j) != 1)
                    continue;
                pii to;
                int tos = s + 1;
                to = pos;
                to.f += i;
                to.s += j;
                if (a[to.f][to.s] == 0 && d[to.f][to.s] == -1) {
                    d[to.f][to.s] = tos;
                    q.ins(mp(tos, to));
                }
            }
        }
    }
    int ans = n + m + 5;
    fi(1, n) {
        fj(1, m) {
            if (d[i][j] <= k && d[i][j] != -1) {
                ans = min(ans, 1 + (min(min(i - 1, j - 1), min(n - i, m - j)) + k - 1) / k);
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 6528