#include <bits\stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f,maxn=804;
queue<int>q;
int a[maxn][maxn],n,m,k,vis[maxn][maxn],sx,sy,mina;
int lrud[5]={0,1,0,-1,0};
void bfs()
{
	int i;
	vis[sx][sy]=0;
	q.push(sx);q.push(sy);
	while(!q.empty())
	{
		int x=q.front();q.pop();int y=q.front();q.pop();
	
		if(vis[x][y]==k)
			continue;
		for(i=1;i<=4;i++)
		{
			int gx=x+lrud[i-1];
			int gy=y+lrud[i];
			if(gx<1 || gy<1 || gx>n || gy>m || vis[x][y]+1>=vis[gx][gy] || a[gx][gy]==1)
				continue;
			vis[gx][gy]=vis[x][y]+1;
			q.push(gx);q.push(gy);

		}
	}
}
int main()
{
	int i,j,ans=0;
	cin>>n>>m>>k;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			vis[i][j]=INF;
			char c;
			cin>>c;
			if(c=='.')
				a[i][j]=0;
			else
				if(c=='#')
					a[i][j]=1;
				else
				{
					sx=i,sy=j;
					a[i][j]=0;
				}
		}
	}
	mina=INF;
	bfs();
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=m;j++)
		{
			if(vis[i][j]!=INF)
			{	mina=min(mina,min((i+k-2)/k,min((j+k-2)/k,min((n-i+k-1)/k,(m-j+k-1)/k))))+1;
			}
		}
	}
	cout<<mina;
	return 0;
}
