#include <bits/stdc++.h>
#define mp  make_pair
using namespace std;

typedef pair <int,int>  PII;
const int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
int n,m,k,a[805][805],dis[805][805],ans;
char ch;
queue <PII> Q;

void work()
{
	scanf("%d %d %d",&n,&m,&k),ans=2*n;
	for (int i=1; i<=n; i++)
		for (int j=1; j<=m; j++)
			{
				do  ch=getchar();  while ((ch!='#')&&(ch!='.')&&(ch!='S'));
				if (ch!='#')  a[i][j]=1;
				if (ch=='S')  dis[i][j]=1,Q.push(mp(i,j));
			}
	while (!Q.empty())
		{
			int x=Q.front().first,y=Q.front().second,d;  Q.pop();
			d=min(min(x-1,n-x),min(y-1,m-y));
			ans=min(ans,(d+k-1)/k+1);
			if (dis[x][y]<=k)
				for (int i=0; i<4; i++)
					if ((a[x+dx[i]][y+dy[i]])&&(!dis[x+dx[i]][y+dy[i]]))
						dis[x+dx[i]][y+dy[i]]=dis[x][y]+1,Q.push(mp(x+dx[i],y+dy[i]));
		}
	printf("%d",ans);
}

int main()
{
	work();
	return 0;
}
