#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
#define INF 10000000
int x[4]={-1,1,0,0};
int y[4]={0,0,1,-1};
int H,W,K,inf=INF;
int checked[800][800]={0};
int map[800][800];
int start[2];
char s;
vector<int> P;


void dfs(int a,int b,int d){
    checked[a][b]a=d;
    P.push_back(fmin(inf,fmin(fmin(a,H-1-a),fmin(b,W-1-b))));
    
    if(d<K){
    for(int i=0;i<4;i++){
        int nx=a+x[i];
        int ny=b+y[i];
        if(map[nx][ny]&&(checked[nx][ny]==0||checked[nx][ny]>d)&&nx>-1&&nx<H&&ny<W&&ny>-1){
            dfs(nx,ny,d+1);
        }
    }
    }
}

int main(void){
    cin>>H>>W>>K;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            cin>>s;
            if(s=='#')map[i][j]=0;
            if(s=='S'){
                map[i][j]=1;
                start[0]=i;
                start[1]=j;
            }
            if(s=='.')map[i][j]=1;
        }
    }
    
    
    dfs(start[0],start[1],0);
    sort(P.begin(),P.end());
    cout<<(P[0]+K-1)/K+1;
}
