#include <queue>
#include <iostream>
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};
int Get(int x,int y)
{
	int X=(x+k-2)/k;
	X=min(X,(y+k-2)/k);
	X=min(X,(n-x+k-1)/k);
	X=min(X,(m-y+k-1)/k);
	return X;
}
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,Get(i,j)+1);
			}
		}
	}
	cout<<mina;
	return 0;
}
