#include<cstdio>
#include<algorithm>
using namespace std;
const int N=805,M=N*N;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int n,m,k,i,j,a[N][N],h,t,qx[M],qy[M],ans;
char s[N];
int main(){
	scanf("%d%d%d",&n,&m,&k);
	for (i=1;i<=n;i++){
		scanf("%s",s+1);
		for (j=1;j<=m;j++){
			if (s[j]=='S') qx[1]=i,qy[1]=j;
			if (s[j]=='#') a[i][j]=1;
		}
	}
	for (i=1;i<=n;i++) a[i][0]=a[i][m+1]=1;
	for (i=1;i<=m;i++) a[0][i]=a[n+1][i]=1;
	t=1;a[qx[1]][qy[1]]=1;ans=1e9;
	for (;h!=t;){
		h++;int x=qx[h],y=qy[h];
		ans=min(ans,min(min(x-1,n-x),min(y-1,m-y)));
		if (a[x][y]==k+1) continue;
		for (i=0;i<4;i++){
			int tx=x+dx[i],ty=y+dy[i];
			if (!a[tx][ty]){
				a[tx][ty]=a[x][y]+1;
				t++;qx[t]=tx;qy[t]=ty;
			}
		}
	}
	printf("%d",1+(ans+k-1)/k);
}