#include "bits/stdc++.h"
using namespace std;
#define int long long
int mod=1e9+7;
 
signed main(){
	int h,w,k;
	cin>>h>>w>>k;
	vector<string> a(h);
	for(int i=0;i<h;i++)cin>>a[i];
	
	queue<int> qx;
	queue<int> qy;
	vector<vector<int> > used(h,vector<int>(w,-1));
	
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(a[i][j]=='S'){
				qx.push(j);
				qy.push(i);
				used[i][j]=0;
			}
		}
	}
	
	int px[]={1,0,-1,0};
	int py[]={0,-1,0,1};
	
	int ans=1e9;
	
	
	while(!qx.empty()){
		int x=qx.front();
		int y=qy.front();
		qx.pop();
		qy.pop();
		if(used[y][x]<=k)ans=min(ans,min(x,min(y,min(w-x-1,h-y-1))));
		for(int i=0;i<4;i++){
			int nx=x+px[i];
			int ny=y+py[i];
			if(nx>=0&&ny>=0&&nx<w&&ny<h&&a[ny][nx]=='.'&&used[ny][nx]==-1){
				used[ny][nx]=used[y][x]+1;
				qx.push(nx);
				qy.push(ny);
			}
		}
	}
	
	int tmp=1;
	if(ans%k!=0)tmp++;
	tmp+=ans/k;
	cout<<tmp<<endl;
}