#include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

using ll = long long;

int main() {
    int H, W, K, si, sj;
    cin >> H >> W >> K;
    vector<string> A(H);
    rep(i, H) {
        cin >> A[i];
        rep(j, W) {
            if (A[i][j] == 'S') {
                si = i;
                sj = j;
            }
        }
    }
    vector<vector<int>> d(H, vector<int>(W, 1e9));
    d[si][sj] = 0;
    queue<vector<int>> que;
    que.push({si, sj});
    int m = 1e9;
    while (!que.empty()) {
        int i = que.front()[0], j = que.front()[1];
        que.pop();
        m = min(m, i);
        m = min(m, H - 1 - i);
        m = min(m, j);
        m = min(m, W - 1 - j);
        if (d[i][j] == K) continue;
        rep(k, 4) {
            vector<int> di = {1, 0, -1, 0};
            vector<int> dj = {0, 1, 0, -1};
            int u = i + di[k];
            int v = j + dj[k];
            if (u < 0 || u >= H || v < 0 || v >= W) continue;
            if (d[u][v] < 1e9) continue;
            if (A[u][v] == '#') continue;
            d[u][v] = d[i][j] + 1;
            que.push({u, v});
        }
    }
    cout << (m + K - 1) / K + 1 << endl;
}