#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int fx[4]={1,0,-1,0},fy[4]={0,1,0,-1};
int qx[640005],qy[640005],d[805][805],h,w,k;
char s[805][805];
int f(int x,int y){
	return (min(x-1,min(h-x,min(y-1,w-y)))+k-1)/k;
}
int main(){
	scanf("%d%d%d",&h,&w,&k);
	for (int i=1;i<=h;i++){
		scanf("%s",s[i]+1);
		for (int j=1;j<=w;j++)
			if (s[i][j]=='S') qx[0]=i,qy[0]=j;
	}
	memset(d,-1,sizeof d);
	int head=0,tail=1,ans=f(qx[0],qy[0]);
	for (d[qx[0]][qy[0]]=0;head!=tail && ans;head++){
		int x=qx[head],y=qy[head];
		if (d[x][y]==k) break;
		for (int i=0;i<4;i++){
			int x2=x+fx[i],y2=y+fy[i];
			if (d[x2][y2]==-1 && s[x2][y2]=='.'){
				d[qx[tail]=x2][qy[tail]=y2]=d[x][y]+1;
				tail++;
				ans=min(ans,f(x2,y2));
			}
		}
	}
	printf("%d\n",ans+1);
}