#include<string>
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long int llint;
#define pub push_back
#define mp make_pair
#define fir first
#define sec second
#define res resize
#define mt make_tuple
const int big=((int)1<<30);
int main(void){
	int H,W,K,i,j,n,sx,sy,x,y,ka,sai=big;
	queue<tuple<int,int,int>> que;//x,y,回数
	vector<string> hey;
	vector<vector<bool>>ot;
	cin>>H>>W>>K;
	hey.resize(H);
	ot.resize(H);
	for(i=0;i<H;i++){cin>>hey[i];}
	for(i=0;i<H;i++){ ot[i].resize(W); for(j=0;j<W;j++){ if(hey[i][j]=='S'){ sx=i;sy=j; } } }
	que.push(mt(sx,sy,K));
	while(!que.empty()){
		x=get<0>(que.front());
		y=get<1>(que.front());
		ka=get<2>(que.front());
		que.pop();
		if(ka==-1){break;}
		if(ot[x][y]){ ot[x][y]=true;continue; }
		sai=min(sai,min(min(x,H-x-1),min(y,W-y-1)));
		if(sai==0){ break; }
		if(hey[x+1][y]!='#'){que.push(mt(x+1,y,ka-1));}
		if(hey[x-1][y]!='#'){que.push(mt(x-1,y,ka-1));}
		if(hey[x][y+1]!='#'){que.push(mt(x,y+1,ka-1));}
		if(hey[x][y-1]!='#'){que.push(mt(x,y-1,ka-1));}
	}
	
	cout<<(sai+K-1)/K+1<<endl;
	return 0;
}