#include<bits/stdc++.h>
#define N 809
#define M 650009
using namespace std;

int m,n,lim,tail,d[N][N],h[M][2]; char s[N][N];
int calc(int x){ return (x)?(x-1)/lim+1:0; }
void ext(int x,int y,int z){
	if (x>0 && x<=m && y>0 && y<=n && s[x][y]=='.' && d[x][y]==-1){
		d[x][y]=z;
		h[++tail][0]=x; h[tail][1]=y;
	}
}
int main(){
	scanf("%d%d%d",&m,&n,&lim);
	int i,j,x,y,z,head=0; tail=1;
	memset(d,-1,sizeof(d));
	for (i=1; i<=m; i++){
		scanf("%s",s[i]+1);
		for (j=1; j<=n; j++) if (s[i][j]=='S'){
			s[i][j]='#';
			h[1][0]=i; h[1][1]=j; d[i][j]=0;
		}
	}
	int ans=1000000000;
	while (head<tail){
		x=h[++head][0]; y=h[head][1]; z=d[x][y]+1;
		if (d[x][y]>lim) continue;
		ans=min(ans,calc(min(min(x-1,m-x),min(y-1,n-y))));
		ext(x-1,y,z); ext(x+1,y,z);
		ext(x,y-1,z); ext(x,y+1,z);
	}
	printf("%d\n",ans+1);
	return 0;
}
