#include <bits/stdc++.h>

using namespace std;

using i64 = long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;


signed main(){
    int h, w, k;
    cin >> h >> w >> k;
    vector<string> s(h);
    for(auto& x : s)
        cin >> x;
    vector<vector<int>> a(h, vector<int>(w));
    int sx = -1, sy = -1;
    for(int i = 0; i < h; ++i)
        for(int j = 0; j < w; ++j){
            a[i][j] = (s[i][j] == '#');
            if(s[i][j] == 'S'){
                sx = i, sy = j;
            }
        }

    vector<int> dx{1, 0, -1, 0};
    vector<int> dy{0, 1, 0, -1};
    auto ptoi = [&](auto x, auto y){return x * w + y;};
    auto itop = [&](auto x){return make_pair(x / w, x % w);};
    vector<vector<int>> range(h, vector<int>(w, MOD));

    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> que;
    range[sx][sy] = 0;
    que.emplace(0, ptoi(sx, sy));
    while(!que.empty()){
        int x, y, dist;
        dist = que.top().first;
        tie(x, y) = itop(que.top().second);
        que.pop();
        if(range[x][y] != dist || dist >= k)
            continue;
        for(int d = 0; d < 4; ++d){
            int nx = x + dx[d];
            int ny = y + dy[d];
            if(nx < 0 || ny < 0 || nx >= h || ny >= w)
                continue;
            if(range[nx][ny] > range[x][y] + 1){
                range[nx][ny] = range[x][y] + 1;
                que.emplace(range[nx][ny], ptoi(nx, ny));
            }
        }
    }


    for(int i = 0; i < h; ++i){
        if(range[i][0] <= k){
            cout << 1 << endl;
            return 0;
        }
        if(range[i][w - 1] <= k){
            cout << 1 << endl;
            return 0;
        }
    }
    for(int i = 0; i < w; ++i){
        if(range[0][i] <= k){
            cout << 1 << endl;
            return 0;
        }
        if(range[h - 1][i] <= k){
            cout << 1 << endl;
            return 0;
        }
    }

    for(int i = 0; i < h; ++i)
        for(int j = 0; j < w; ++j)
            if(range[i][j] != MOD){
                que.emplace(0, ptoi(i, j));
                range[i][j] = 0;
            }

    while(!que.empty()){
        int x, y, dist;
        dist = que.top().first;
        tie(x, y) = itop(que.top().second);
        que.pop();
        if(range[x][y] != dist)
            continue;
        for(int d = 0; d < 4; ++d){
            int nx = x + dx[d];
            int ny = y + dy[d];
            if(nx < 0 || ny < 0 || nx >= h || ny >= w)
                continue;
            if(range[nx][ny] > range[x][y] + 1){
                range[nx][ny] = range[x][y] + 1;
                que.emplace(range[nx][ny], ptoi(nx, ny));
            }
        }
    }

    int val = MOD;
    for(int i = 0; i < h; ++i){
        val = min(val, range[i][0]);
        val = min(val, range[i][w - 1]);
    }
    for(int i = 0; i < w; ++i){
        val = min(val, range[0][i]);
        val = min(val, range[h - 1][i]);
    }
    cout << (val + k - 1) / k + 1 << endl;

}
