#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e3+5;
char s[N][N];
int n,m,k;
int sx,sy;
int z[N][N];
int ans=1<<30;
typedef pair<int,int>pi;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
queue<pi>q;
bool vis[N][N];
int deep[N][N];
void bfs(){
    q.push(pi(sx,sy));
    vis[sx][sy]=1;
    while(!q.empty()){
        int x=q.front().first,y=q.front().second;
        q.pop();
        ans=min(ans,z[x][y]);
        if(deep[x][y]==k)
            continue;
        for(int i=0;i<4;i++){
            int xx=x+dir[i][0],yy=y+dir[i][1];
            if(xx<=0||yy<=0||xx>n||yy>m||vis[xx][yy]||s[xx][yy]=='#')
                continue;
            vis[xx][yy]=1;
            deep[xx][yy]=deep[x][y]+1;
            q.push(pi(xx,yy));
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=n;i++){
        scanf("%s",s[i]+1);
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(s[i][j]=='S')
                sx=i,sy=j;
            z[i][j]=min(min(i-1,j-1),min(n-i,m-j));
            z[i][j]=(z[i][j]+k-1)/k;
        }
    bfs();
    printf("%d\n",ans+1);
}
