#include<cstdio>
#include<algorithm>
using namespace std;
int Q[1010000][2], head, tail, D[810][810];
int n, m, K, sx, sy;
char p[810][810];
void Ins(int x, int y, int d){
    if(D[x][y]<=d || p[x][y]!='.')return;
    D[x][y]=d;
    tail++;
    Q[tail][0]=x,Q[tail][1]=y;
}
int main(){
    int i, j, x, y;
    scanf("%d%d%d",&n,&m,&K);
    for(i=1;i<=n;i++){
        scanf("%s",p[i]+1);
        for(j=1;j<=m;j++){
            if(p[i][j]=='S')sx=i,sy=j,p[i][j]='.';
            D[i][j]=1e9;
        }
    }
    Ins(sx,sy,0);
    while(head < tail){
        head++;
        x = Q[head][0], y = Q[head][1];
        Ins(x+1,y,D[x][y]+1);
        Ins(x,y+1,D[x][y]+1);
        Ins(x-1,y,D[x][y]+1);
        Ins(x,y-1,D[x][y]+1);
    }
    int res = 1e9;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            if(D[i][j] <= K){
                res = min(res,1 + (min(min(i-1, n-i), min(j-1, m-j)) + K-1)/K);
            }
        }
    }
    printf("%d\n",res);
}