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

#define rep(i,l,r) for(int i=l;i<=r;++i)
const int N=800+5,inf=1e9,fx[4]={-1,0,1,0},fy[4]={0,-1,0,1};
char s[N][N];
int g[N][N];
pair<int,int>q[N*N];int tail;

int main()
{
	//freopen("1.in","r",stdin);
	int n,m,k;
	cin>>n>>m>>k;
	rep(i,1,n)scanf("%s",s[i]+1);
	rep(i,1,n)
	rep(j,1,m)
	if(s[i][j]!='#')g[i][j]=inf;
	rep(i,1,n)
	rep(j,1,m)
	if(s[i][j]=='S')
	{
		tail=1;
		q[1]=make_pair(i,j);
		g[i][j]=0;
	}
	int ans=inf;
	rep(head,1,tail)
	{
		int x=q[head].first,y=q[head].second;
		ans=min(ans,min(min(min(x-1,y-1),n-x),m-y));
		if(g[x][y]==k)continue;
		rep(i,0,3)
		{
			int tx=x+fx[i],ty=y+fy[i];
			if(g[tx][ty]==inf)
			{
				g[tx][ty]=g[x][y]+1;
				q[++tail]=make_pair(tx,ty);
			}
		}
	}
	cout<<(ans+k-1)/k+1;
}