#include<bits/stdc++.h>
using namespace std;
int n,m,k;
char grid[1000][1000];
int steps[1000][1000];
queue<pair<int,int> > q;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
bool ok(int i,int j){
	return i>=0&&i<n&&j>=0&&j<m;
}
void BFS(int x,int y){
	memset(steps,0x3f,sizeof(steps));
	q.push(make_pair(x,y));
	steps[x][y]=0;
	while(!q.empty()){
		pair<int,int> cur=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int nx=cur.first+dx[i];
			int ny=cur.second+dy[i];
			if(ok(nx,ny)&&grid[nx][ny]!='#'&&steps[nx][ny]>=1e9){
				steps[nx][ny]=steps[cur.first][cur.second]+1;
				q.push(make_pair(nx,ny));
			}
		}
	}
}
int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin>>n>>m>>k;
	pair<int,int> stt;
	for(int i=0;i<n;i++)
	{
		scanf("%s",grid[i]);
		for(int j=0;j<m;j++){
			if(grid[i][j]=='S')
				stt=make_pair(i,j);
		}
	}
	BFS(stt.first,stt.second);
	int res=1e9;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(grid[i][j]!='#'&&steps[i][j]<=k){
				int mmin=min(min(i,j),min(n-i-1,m-j-1));
				res=min(res,(mmin+k-1)/k+1);
			}
		}
	}
	cout<<res<<endl;
	return 0;
}
