#include<bits/stdc++.h>
#include<boost/variant.hpp>
using namespace std;
typedef long long ll;
typedef vector<boost::variant<bool, ll, int, string, double, char*, const char*>> any;
template<typename T> inline void pr(const vector<T> &xs){
	for(int i=0; i<xs.size()-1; i++) cout<<xs[i]<<" ";
	(xs.empty()?cout:(cout<<xs[xs.size()-1]))<<endl;
}
template<typename T> inline void debug(const vector<T> &xs){
#ifdef DEBUG
	pr(xs);
#endif
}

int main(){
	int H, W, K;
	cin >> H >> W >> K;
	vector<string> A(H);
	for(int i=0; i<H; i++) cin >> A[i];

	int sx, sy;
	for(int i=0; i<H; i++) for(int j=0; j<W; j++){
		if(A[i][j]=='S'){
			sx = i;
			sy = j;
		}
	}

	vector<vector<bool>> visit(H, vector<bool>(W));
	vector<pair<int, int>> q(1, {sx, sy});
	int px, py;
	int mdiff = 1e9;
	for(int k=0; k<=K; k++){
		vector<pair<int, int>> next;
		for(auto p: q){
			int x=p.first;
			int y=p.second;
			if(min(min(x, y), min(H-x-1, W-y-1)) < mdiff){
				mdiff = min(min(x, y), min(H-x-1, W-y-1));
				px = x;
				py = y;
			}
			visit[x][y] = true;
			int dx[] = {1, -1, 0, 0};
			int dy[] = {0, 0, 1, -1};
			for(int n=0; n<4; n++){
				int nx = x+dx[n];
				int ny = y+dy[n];
				if(nx<0||H<=nx||ny<0||W<=ny) continue;
				if(A[nx][ny]!='.') continue;
				if(visit[nx][ny]) continue;
				next.emplace_back(nx, ny);
			}
		}
		q = next;
	}

	cout << 1+(mdiff+K-1)/K << endl;

	return 0;
}
