#include<bits/stdc++.h>
using namespace std;
const int INF = 1000000007;

int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, -1, 0};

void dfs(int i, int j, vector<vector<bool> > &G, vector<pair<int, int> > &ans){

    ans.push_back({i, j});
    if(k == 0) return;
    for(int h = 0; h < 4; h++){
        int ni = i + di[h];
        int nj = j + dj[h];

        if(G[ni][nj]){
            G[i][j] = false;
            dfs(ni, nj, G, ans, k - 1);
        }
    }
}

int main(){

    int h, w, k; cin >> h >> w >> k;
    vector<vector<bool> > G(h + 2, vector<bool> (w + 2, false));


    int si, sj;
    for(int i = 1; i <= h; i++){
        for(int j = 1; j <= w; j++){
            char in; cin >> in;
            if(in == '#') continue;

            G[i][j] = true;
            if(in == 'S'){
                si = i;
                sj = j;
            }
        }
    }

    vector<pair<int, int> > ans;
    G[si][sj] = false;
    dfs(si, sj, G, ans, k);

    int anss = INF;
    for(int i = 0; i < ans.size(); i++){
        int tmp = min(min(ans[i].first - 1, ans[i].second - 1), min(h - ans[i].first, w - ans[i].second));
        anss = min(anss, 2 + (tmp - 1) / k);
        if(tmp == 0) anss = 1;
    }
    

    cout << anss << endl;
    return 0;
}