#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef std::vector<int>vi;
typedef std::vector<string>vs;
typedef std::vector<double>vd;
typedef std::vector<ll>vll;
typedef vector<vi>vvi;
typedef vector<vvi>vvvi;
typedef vector<vll>vvll;
typedef std::pair<int,int>pi;
typedef vector<pi>vpi;
typedef vector<vpi>vvpi;

const int mmax=810;
int ans=1E9+7;
int h,w,k;
bool a[mmax][mmax];
int sx,sy;
bool b[mmax][mmax];

void dfs(int i,int j,int p){
	if(i>=0&&i<h&&j>=0&&j<w&&a[i][j]==true){
		if(b[i][j]==false){
			b[i][j]==true;
			int d=min(min(min(i,j),(h-i-1)),(w-j-1));
			d=((d+k-1)/k)+1;
			ans=min(ans,d);
			if(p!=k){
				dfs(i-1,j,p+1);
				dfs(i,j-1,p+1);
				dfs(i+1,j,p+1);
				dfs(i,j+1,p+1); 
			}
		}	
	}
	
}

int main(){
	cin>>h>>w>>k;
	for(int i=0;i<h;i++){
		string c;
		cin>>c;
		for(int j=0;j<w;j++){
			if(c[j]=='#'){
				a[i][j]=false;
			}else{
				a[i][j]=true;
				if(c[j]=='S'){
					sx=i;
					sy=j;
				}
			}
		}
	}
	dfs(sx,sy,0);
	cout<<ans;
	return 0;
}
