#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
#define rep(i,n) for (ll i=0;i<n;++i)
#define rep2(i,a,b) for (ll i=a;i<b;++i)
const ll MOD=1e9+7;
const ll INF=1e9;
const ll IINF=1e18;
const double EPS=1e-8;
const double pi=acos(-1);

template<class T> inline bool chmin(T &a,T b){
    if (a>b){
        a=b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T &a,T b){
    if (a<b){
        a=b;
        return true;
    }
    return false;
}

int main(){
    int H,W,K;
    cin >> H >> W >> K;
    vector<string> A(H);
    rep(i,H) cin >> A[i];
    int sh,sw;
    rep(i,H) rep(j,W) if (A[i][j]=='S') sh=i,sw=j;
    int dh[4]={1,0,-1,0};
    int dw[4]={0,1,0,-1};
    queue<Pii> que;
    que.push(Pii(sh,sw));
    vector<vector<int>> d(H,vector<int>(W,-1));
    d[sh][sw]=0;
    int ans=INF;
    while(!que.empty()){
        Pii p=que.front();
        que.pop();
        int h=p.first,w=p.second;
        int x=(d[h][w]+min(h,min(H-1-h,min(w,W-1-w)))+K-1)/K;
        if (d[h][w]<K) x=1+(min(h,min(H-1-h,min(w,W-1-w)))+K-1)/K;
        chmin(ans,x);
        rep(j,4){
            int nh=h+dh[j],nw=w+dw[j];
            if (nh<0||nh>=H||nw<0||nw>=W) continue;
            if (A[nh][nw]=='#') continue;
            if (d[nh][nw]!=-1) continue;
            d[nh][nw]=d[h][w]+1;
            que.push(Pii(nh,nw));
        }
    }
    cout << ans << endl;
}