#include<cstdio>
#include<algorithm>
#include<queue>
#define MAXN 806
using namespace std;
int n,m,k,step[MAXN*MAXN],way[4][2]={{-1,0},{1,0},{0,1},{0,-1}},s,ans;
char a[MAXN][MAXN];
queue<int> q;
int main()
{
	//freopen("room.in","r",stdin);
	//freopen("room.out","w",stdout);
	scanf("%d%d%d",&n,&m,&k);
	ans=n*m*666;
	for(int i=1;i<=n;i++)
		scanf("%s",a[i]+1);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(a[i][j]=='S')
			{
				if(i==1||i==n||j==1||j==m)
				{
					printf("0");
					return 0;
				}
				s=(i-1)*m+j;
				break;
			}
	step[s]=1;
	q.push(s);
	while(!q.empty())
	{
		int now=q.front();
		q.pop();
		if(step[now]==k+1) continue;
		int nowx=(now-1)/m+1;
		int nowy=now-(nowx-1)*m;
		for(int i=0;i<4;i++)
		{
			int nex=nowx+way[i][0];
			int ney=nowy+way[i][1];
			int ne=(nex-1)*m+ney;
			if(step[ne]||a[nex][ney]!='.') continue;
			step[ne]=step[now]+1;
			q.push(ne);
		}
	}
	for(int i=1;i<=n*m;i++)
	{
		if(step[i])
		{
			int x=(i-1)/m+1;
			int y=i-(x-1)*m;
			int p=min(min(x-1,n-x),min(y-1,m-y));
			int q=p/k;
			if(p%k) q++;
			ans=min(ans,q+1);
		}
	}
	printf("%d",ans);
}