#include<bits/stdc++.h>
using namespace std;
#define N 805
char mp[N][N];
int n,m,k;
int ans=1e9;
int xx[4]={-1,0,1,0},yy[4]={0,-1,0,1};

struct msg{int x,y,step;};
queue<msg >q;
int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(mp[i][j]=='S')
                q.push((msg){i,j,0});
    while(!q.empty()){
        msg v=q.front();q.pop();
        int x=v.x,y=v.y;
        ans=min(ans,(min(min(x-1,n-x),min(y-1,m-y))+k-1)/k+1);
        for(int i=0;i<4;i++){
            int nx=x+xx[i],ny=y+yy[i];
            if(1<=nx&&nx<=n&&1<=ny&&ny<=m&&mp[nx][ny]=='.'&&v.step<k){
                mp[nx][ny]='x';
                q.push((msg){nx,ny,v.step+1});
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}