#include<bits/stdc++.h>
using namespace std;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
int n,m,k,mn,dist[805][805];
char s[805][805];
queue<pair<int,int> >q;
int dis(int x,int y){return (min(min(x,y),min(n-1-x,m-1-y))+k-1)/k;}
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	memset(dist,-1,sizeof(dist));
	for(int i=0;i<n;i++)
	{
		scanf("%s",s[i]);
		for(int j=0;j<m;j++)if(s[i][j]=='S')
		{
			q.push(make_pair(i,j));
			dist[i][j]=0;
			mn=dis(i,j)+1;
		}
	}
	while(!q.empty())
	{
		int x=q.front().first,y=q.front().second;
		q.pop();
		if(dist[x][y]==k)break;
		for(int i=0;i<4;i++)
		{
			int xx=x+dx[i],yy=y+dy[i];
			if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy]=='.'&&dist[xx][yy]==-1)
			{
				dist[xx][yy]=dist[x][y]+1;
				q.push(make_pair(xx,yy));
				mn=min(mn,dis(xx,yy)+1);
			}
		}
	}
	printf("%d\n",mn);
	return 0;
}