#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
#define REP(i, x, y) for(ll i = x; i < y; i++)
#define PER(i, x, y) for(ll i = x; i > y; i--)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

ll H, W, K;

bool bo[1000][1000];

ll boone[1000][1000];

ll num[1000][1000];

void BFS01(ll sth, ll stw) {
    deque<plglg> que;
    num[sth][stw] = 0;
    que.push_front(plglg(sth, stw));
    while (que.size() > 0) {
        plglg base = que.front();
        ll basex = base.first;
        ll basey = base.second;
        que.pop_front();
        REP(i, 0, 4) {
            ll x = basex + dx[i];
            ll y = basey + dy[i];
            if (x >= 0 && x < H && y >= 0 && y < W) {
                if (num[x][y] == -1) {
                    if (boone[basex][basey] >= 0 && boone[x][y] >= 0) {
                        num[x][y] = num[basex][basey];
                        que.push_front(plglg(x, y));
                    } else {
                        num[x][y] = num[basex][basey] + 1;
                        que.push_back(plglg(x, y));
                    }
                }
            }
        }
    }
}

ll calc(ll x) {
    return (x + K - 1) / K + 1;
}

int main() {
    cin >> H >> W >> K;
    ll sth, stw;
    REP(i, 0, H) {
        string s;
        cin >> s;
        REP(j, 0, W) {
            if (s[j] == 'S') {
                sth = i;
                stw = j;
                bo[i][j] = true;
            } else if (s[j] == '.') {
                bo[i][j] = true;
            } else {
                bo[i][j] = false;
            }
        }
    }
    REP(i, 0, 1000) {
        fill(boone[i], boone[i] + 1000, -1);
        fill(num[i], num[i] + 1000, -1);
    }
    queue<plglg> que;
    que.push(plglg(sth, stw));
    boone[sth][stw] = 0;
    while (!que.empty()) {
        plglg p = que.front();
        que.pop();
        ll tmp = boone[p.first][p.second];
        if (tmp < K) {
            REP(i, 0, 4) {
                ll x = p.first + dx[i];
                ll y = p.second + dy[i];
                if (x >= 0 && x < H && y >= 0 && y < W) {
                    if (bo[x][y] && boone[x][y] == -1) {
                        boone[x][y] = tmp + 1;
                        que.push(plglg(x, y));
                    }
                }
            }
        }
    }
    BFS01(sth, stw);
    ll ans = 1e7;
    REP(i, 0, H) {
        ans = min(ans, calc(num[i][0]));
        ans = min(ans, calc(num[i][W - 1]));
    }
    REP(i, 0, W) {
        ans = min(ans, calc(num[0][i]));
        ans = min(ans, calc(num[H - 1][i]));
    }

    // REP(i, 0, H) {
    //     REP(j, 0, W) {
    //         cout << boone[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    // REP(i, 0, H) {
    //     REP(j, 0, W) {
    //         cout << num[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    cout << ans << endl;
}


Measured memory_bytes: 22016