#include<bits/stdc++.h>
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
using namespace std;
using ll = long long;
const ll MOD = (ll)1e9+7;
//const ll MOD = 998244353;
//const ll MOD = 924844033;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
const double EPS = 1e-5;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

#ifdef DEBUG
#define OUTPUT(x) (output(x), outendl())
#else
#define OUTPUT(x) (void)0
#endif

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){ return modpow(x, MOD-2); }
ll in(){ ll x; scanf("%lld",&x); return x; }
string in_s(){ string res;cin >> res;return res; }
char in_c(){char x;cin >> x;return x;}
bool was_output = false;
template<class t>
void output(t a){ if(was_output)cout << " "; cout << a; was_output = true; }
void outendl(){ was_output = false; cout << endl; }

int h;
int w;
int k;
vvector<bool> wall;
pii start;

void input(){
    h = in();
    w = in();
    k = in();
    wall.resize(h,vector<bool>(w));
    rep(i,h){
        rep(j,w){
            char d = in_c();
            wall[i][j] = d == '#';
            if(d=='S'){
                start = pii(i,j);
            }
        }
    }
}

#define pii2p(x) x.first][x.second

int mx[]={1,0,-1,0}, my[]={0,1,0,-1};
int solve(){
    vvector<int> fast(h,vector<int>(h,INF));
    queue<pii> que;
    que.emplace(start);
    fast[pii2p(start)] = k;
    auto is_in = [](pii p){
        return 0 <= p.first && p.first < h && 0 <= p.second && p.second < w;
    };

    auto is_wall = [&](pii p){
        if(!is_in(p))return false;
        return (bool)wall[pii2p(p)];
    };
    auto is_goal = [](pii p){
        return p.first == 0 || p.first == h-1 || p.second == 0 || p.second == w-1;
    };
    while(que.size()){
        pii np = que.front();
        que.pop();
        if(fast[pii2p(np)]==0)continue;
        rep(i,4){
            pii nep(np.first+my[i],np.second+mx[i]);
            if(is_wall(nep))continue;
            int nt = fast[pii2p(np)] - 1;
            if(fast[pii2p(nep)]!=INF && nt <= fast[pii2p(nep)])continue;
            fast[pii2p(nep)] = nt;
            que.emplace(nep);
            if(is_goal(nep))return 1;
        }
    }
    rep(i,h){
        rep(j,w){
            if(fast[i][j]!=INF){
                que.emplace(i,j);
            }
        }
    }

    fast.assign(h,vector<int>(w,INF));

    [&](queue<pii> line){
        while(line.size()){
            pii d = line.front();
            line.pop();
            fast[pii2p(d)] = 0;
        }
    }(que);

    int res = INF;
    while(que.size()){
        pii np = que.front();
        que.pop();
        rep(i,4){
            pii nep(np.first+my[i],np.second+mx[i]);
            if(!is_in(nep))continue;
            int nt = fast[pii2p(np)] + 1;
            if(nt >= fast[pii2p(nep)])continue;
            fast[pii2p(nep)] = nt;
            que.push(nep);
            if(is_goal(nep)){
                chmin(res,(nt + k - 1) / k + 1);
            }
        }
    }
    return res;
}

int main(){
    input();
    cout << solve() << endl;
	return 0;
}


Measured memory_bytes: 3456