#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 100000000



int main(){
	
	int H,W,K;
	cin>>H>>W>>K;
	
	vector<string> S(H);
	for(int i=0;i<H;i++){
		cin>>S[i];
	}
	
	int sx,sy;
	
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			if(S[i][j]=='S'){
				sx=j;
				sy=i;
				break;
			}
		}
	}
	
	vector<vector<int>> dis(H,vector<int>(W,Inf));
	dis[sy][sx]=0;
	queue<pair<int,int>> Q;
	Q.emplace(sy,sx);
	
	while(Q.size()!=0){
		int y = Q.front().first;
		int x = Q.front().second;
		Q.pop();
		for(int i=0;i<4;i++){
			int yy = y;
			int xx = x;
			if(i==0)yy++;
			if(i==1)yy--;
			if(i==2)xx++;
			if(i==3)xx--;
			if(yy<0||yy>=H||xx<0||xx>=W)continue;
			if(S[yy][xx]=='#')continue;
			if(dis[yy][xx]!=Inf)continue;
			dis[yy][xx] = dis[y][x] + 1;
			Q.emplace(yy,xx);
		}
	}
	
	int ans = Inf;
	
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			if(dis[i][j]>K)continue;
			int a = min({i,j,H-1-i,W-1-j});
			ans = min(ans,(a+K-1)/K);
		}
	}
	cout<<ans+1<<endl;


	return 0;
}