#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>
#include <functional>
#include <chrono>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const double EPS = 1e-9;

const int dh[] = {0, -1, 0, 1};
const int dw[] = {1, 0, -1, 0};
const int MAX = 888;
int H, W, K;
string A[MAX];
char strbuf[MAX];
int vis[MAX][MAX];
string next() {
scanf("%s", strbuf);
return strbuf;
}

bool inside(int h, int w) {
    return 0 <= h && h < H && 0 <= w && w < W;
}
bool inside(PII p) {
    return inside(p.first, p.second);
}
bool isGoal(int h, int w) {
    return h == 0 || w == 0 || h == H - 1 || w == W - 1;
}
bool isGoal(PII p) {
    return isGoal(p.first, p.second);
}

int main() {
    memset(vis, -1, sizeof(vis));
    scanf("%d%d%d", &H, &W, &K);
    PII start;
    REP(i, H) {
        A[i] = next();
        REP(j, W) if (A[i][j] == 'S') {
            start = MP(i, j);
        }
    }
    queue<pair<int, PII>> q;
    q.push(MP(K, start));
    while (!q.empty()) {
        auto el = q.front(); q.pop();
        if (isGoal(el.second)) {
            cout << 1 << endl;
            return 0;
        }
        if (!chmax(vis[el.second.first][el.second.second], el.first)) {
            continue;
        }
        if (el.first) REP(i, 4) {
            PII next = MP(el.second.first + dh[i], el.second.second + dw[i]);
            if (!inside(next)) {
                continue;
            }
            if (A[next.first][next.second] == '#') {
                continue;
            }
            q.push(MP(el.first - 1, next));
        }
    }
    int ans = INF;
    REP(i, H) {
        REP(j, W) {
            if (vis[i][j] != -1) {
                chmin(ans, i / K + (i % K != 0) + 1);
                chmin(ans, j / K + (j % K != 0) + 1);
                chmin(ans, (H - i - 1) / K + ((H - i - 1) % K != 0) + 1);
                chmin(ans, (W - j - 1) / K + ((W - j - 1) % K != 0) + 1);
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 3968