#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.141592653589793
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = a; i < (n); i++)
#define rrep(i, n, k) for (int i = (n); i >= (k); i--);
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}
const int MOD = 1e9+ 7;
const int INF = 2e18;

struct info {
    int y, x, count;
};

signed main() {
    int H, W, K;
    cin >> H >> W >> K;
    vector<vector<char>> A(H, vector<char>(W));
    pair<int, int> start;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> A[i][j];
            if (A[i][j] == 'S') {
                start = {i ,j};
            }
        }
    }
    int ans = INF;
    vector<vector<bool>> check(H, vector<bool>(W, false));
    stack<info> explore;
    explore.push({start.first, start.second, 0});
    while (!explore.empty()) {
        auto now = explore.top();
        explore.pop();
        if (now.count > K) break;
        int shortestY = min(now.y, H - 1 - now.y);
        int shortestX = min(now.x, W - 1 - now.x);
        shortestY = shortestY / K + (shortestY % K != 0);
        shortestX = shortestX / K + (shortestX % K != 0);
        ans = min(ans, 1 + min(shortestY, shortestX));
        for (int x = -1; x <= 1; x++) {
            for (int y = -1; y <= 1; y++) {
                if (x == y) continue;
                if (A[now.y + y][now.x + x] == '#') continue;
                if (check[now.y + y][now.x + x]) continue;
                explore.push({now.y + y, now.x + x, now.count + 1});
                check[now.y + y][now.x + x] = true;
            }
        }
    }
    cout << ans << endl;
}