#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
int main(){
    int h,w,k;
    scanf("%d%d%d",&h,&w,&k);
    char a[h][w];
    int sx, sy;
    for (int i = 0; i < h; i++){
        for (int j = 0; j < w; j++){
            scanf(" %c",&a[i][j]);
            if (a[i][j] == 'S'){
                sx = i, sy = j;
            }
        }
    }
    queue<ii> q;
    int dist[h][w];
    memset(dist,-1,sizeof(dist));
    q.push(ii(sx,sy));
    dist[sx][sy] = 0;
    int ans = h*w;
    while (q.size()){
        ii cur = q.front(); q.pop();
        int x = cur.first, y = cur.second;
        ans = min(ans,min(min(x,h-x-1),min(y,w-y-1)));
        if (dist[x][y] == k) continue;
        for (int k = 0; k < 4; k++){
            int nx = x+dx[k], ny = y+dy[k];
            if (nx >= 0 && nx < h && ny >=0 && ny < w && a[nx][ny] == '.' && dist[nx][ny] == -1){
                dist[nx][ny] = dist[x][y]+1;
                q.push(ii(nx,ny));
            }
        }
    }
    //printf("%d ",ans);
    printf("%d",1+(ans-1)/k+(ans!=0));
}
