#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; 
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef pair<int,int> P;
typedef pair<int,P> PP;
#define top front

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    

    int h,w,k;cin>>h>>w>>k;
    string a[h];
    rep(i,h) cin>>a[i];

    queue<PP> q1,q2;
    bool checked[h][w]={};
    rep(i,h){
        rep(j,w){
            if(a[i][j]=='S'){
                q1.push(PP(k,P(i,j)));
                checked[i][j]=true;
            }
        }
    }

    int magic=1;
    while( (!q1.empty()) || (!q2.empty()) ){
        
        queue<PP> q1nxt,q2nxt;
        while(!q1.empty()){
            auto now=q1.top();q1.pop();
            
            rep(i,4){
                int ni=now.second.first+dx[i],nj=now.second.second+dy[i];
                if(ni<0||ni>=h||nj<0||nj>=w){
                    cout<<magic<<endl;
                    return 0;
                }
                if(checked[ni][nj]) continue;
                if(a[ni][nj]=='#'){
                    q2.push(PP(k,P(ni,nj)));
                }
                else if(k==0){
                    q1nxt.push(PP(k,P(ni,nj)));
                }
                else{
                    q1.push(PP(now.first-1,P(ni,nj)));
                }
                checked[ni][nj]=true;
            }
        }
        swap(q1,q1nxt);

        while(!q2.empty()){
            auto now=q1.top();q1.pop();
            
            rep(i,4){
                int ni=now.second.first+dx[i],nj=now.second.second+dy[i];
                if(ni<0||ni>=h||nj<0||nj>=w){
                    cout<<magic+1<<endl;
                    return 0;
                }
                if(checked[ni][nj]) continue;
                if(a[ni][nj]=='.'){
                    q1.push(PP(k,P(ni,nj)));
                }
                else if(k==0){
                    q2nxt.push(PP(k,P(ni,nj)));
                }
                else{
                    q2.push(PP(now.first-1,P(ni,nj)));
                }
                checked[ni][nj]=true;
            }
        }
        swap(q2,q2nxt);
        magic++;
    }
    return 0;
}
