#include<cstdio>
#include<algorithm>
using namespace std;
#define MN 800
char s[MN+5][MN+5];
int qx[MN*MN+5],qy[MN*MN+5],qn,d[MN+5][MN+5];
const int o[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int main()
{
	int n,m,k,i,j,x,y,ans=MN;
	scanf("%d%d%d",&n,&m,&k);
	for(i=1;i<=n;++i)
	{
		scanf("%s",s[i]+1);
		for(j=1;j<=m;++j)if(s[i][j]=='S')d[qx[0]=i][qy[0]=j]=1;
	}
	for(i=0;i<=qn;++i)
	{
		ans=min(ans,(min(min(qx[i]-1,n-qx[i]),min(qy[i]-1,m-qy[i]))+k-1)/k);
		if(d[qx[i]][qy[i]]<=k)for(j=0;j<4;++j)
		{
			x=qx[i]+o[j][0];y=qy[i]+o[j][1];
			if(s[x][y]=='.'&&!d[x][y])d[x][y]=d[qx[i]][qy[i]]+1,qx[++qn]=x,qy[qn]=y;
		}
	}
	printf("%d",ans+1);
}