#include <bits/stdc++.h>
#define REP(i,n,N) for(int i=(n);i<(int)N;i++)
#define p(s) cout<<(s)<<endl
#define ck(n,a,b) ((a)<=(n)&&(n)<(b))
#define F first
#define S second
typedef long long ll;
using namespace std;
const int inf=1e9;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
bool visit[810][810];
int main(){
	int H,W,K;
	cin>>H>>W>>K;
	char A[810][810];
	int sx,sy;
	REP(i,0,H) REP(j,0,W) {
		cin>>A[i][j];
		if(A[i][j]=='S'){
			sx=j;
			sy=i;
		}
	}
	queue<pair<int,pair<int,int>>> q;
	q.push({0,{sx,sy}});
	visit[sx][sy]=true;
	int mn=inf;
	while(!q.empty()){
		auto now=q.front();q.pop();
		REP(i,0,4){
			int nx=now.S.F+dx[i],ny=now.S.S+dy[i];
			int cost=now.F+1;
			if(!ck(ny,0,H)||!ck(nx,0,W)) continue;
			if(A[ny][nx]=='#'||visit[ny][nx]) continue;
			visit[ny][nx]=true;
			mn=min(mn,min(abs(nx-0),abs(W-1-nx)));
			mn=min(mn,min(abs(ny-0),abs(H-1-ny)));
			if(cost<K) q.push({cost,{nx,ny}});
		}
	}
	if(mn==inf) {
		mn=min(abs(sx-0),abs(W-1-sx));
		mn=min(mn,min(abs(sy-0),abs(H-1-sy)));
	}
	int ans=1;
	while(mn>0){
		ans++;
		mn-=K;
	}
	p(ans);
	return 0;
}
