#include<bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
ll n,m,k,i,j,d[1010][1010],has;
char a[1010][1010];
queue<pair<ll,ll> > q;
ll dx[4]={0,0,-1,1};
ll dy[4]={-1,1,0,0};
int main()
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m>>k;
	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
		{
			cin>>a[i][j];
			if(a[i][j]=='S')
			{
				q.push(mp(i,j));
			}
			else
				d[i][j]=1e18;
		}
	has=1e18;
	while(!q.empty())
	{
		ll x=q.front().fi;
		ll y=q.front().se;
		if(d[x][y]<=k)
		{
		//	cout<<x<<" "<<y<<"_\n";
			has=min(has,min(min(x-1,y-1),min(n-x,m-y)));
		}
		q.pop();
		for(i=0;i<4;i++)
		{
			ll nx=x+dx[i];
			ll ny=y+dy[i];
			if(a[nx][ny]=='.'&&d[nx][ny]==1e18)
			{
		//		cout<<nx<<" "<<ny<<"\n";
				d[nx][ny]=d[x][y]+1;
				q.push(mp(nx,ny));
			}
		}
	}
	//cout<<has<<"\n";
	ll tam=0;
	if((has%k)!=0)
		tam=1;
	has=(has/k)+tam;
	has++;
	cout<<has<<"\n";
}