#include<bits/stdc++.h>
using namespace std;
#define N 808
#define pii pair<int,int>
#define tri pair<pii,int>
#define mp make_pair
#define F first
#define S second 

int n,m,k,ans;
char s[N][N];
bool vis[N][N];
queue<tri> q;
bool inRange(int x,int y)
{
	return x>=0 && x<n && y>=0 && y<m && s[x][y]!='#';
}

int main()
{
	
	cin>>n>>m>>k;
	for(int i=0;i<n;i++)
	{
		cin>>s[i];
		for(int j=0;j<m;j++)
			if(s[i][j]=='S')
			{
				q.push(mp(mp(i,j),0));
				vis[i][j]=1;
			}
	}
	
	ans=1e9;
	while(!q.empty())
	{
		int x=q.front().F.F;
		int y=q.front().F.S;
		int step=q.front().S;
		q.pop();
		ans=min(ans,min(min(x,y),min(n-1-x,m-1-y)));
		for(int dx=-1;dx<=1;dx++)
		for(int dy=-1;dy<=1;dy++)
		{
			if(abs(dx)+abs(dy)!=1) continue;
			if(!inRange(x+dx,y+dy)) continue;
			if(vis[x+dx][y+dy]) continue;
			if(step==k) continue;
			vis[x+dx][y+dy]=1;
			q.push(mp(mp(x+dx,y+dy),step+1));
		}
	}
	cout<<(ans+k-1)/k+1<<'\n';
	
	return 0;
}