#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

Int h, w, k;
vector<string> a;
vector<vector<Int>> v;
Int dx[4] = {0, 0, 1, -1};
Int dy[4] = {1, -1, 0, 0};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> h >> w >> k;
	a.resize(h);
	for(auto &i:a) cin >> i;
	v.resize(h, vector<Int>(w, -1));
	using P = pair<Int, Int>;
	queue<P> q;
	for(Int i=0; i<h; ++i)
		for(Int j=0; j<w; ++j)
			if(a[i][j] == 'S'){
				v[i][j] = k;
				q.push(P(i, j));
			}
	while(!q.empty()){
		P p = q.front();
		q.pop();
		Int x = p.first;
		Int y = p.second;
		for(Int i=0; i<4; ++i){
			Int nx = x + dx[i];
			Int ny = y + dy[i];
			if(0 <= nx && nx < h && 0 <= ny && ny < w && a[nx][ny] == '.' && v[x][y] - 1 > v[nx][ny]){
				v[nx][ny] = v[x][y] - 1;
				if(v[nx][ny] > 0) q.push(P(nx, ny));
			}
		}
	}
	Int ans = 1e9;
	for(Int i=0; i<h; ++i)
		for(Int j=0; j<w; ++j)
			if(v[i][j] != -1){
				if(i == 0 || j == 0 || i == h-1 || j == w-1) ans = 1;
				else ans = min({ans, 1+(i+k-1)/k, 1+(j+k-1)/k, 1+(h-1-i+k-1)/k, 1+(w-1-j+k-1)/k});
			}
	cout << ans << "\n";
//	for(Int i=0; i<h; ++i)
//		for(Int j=0; j<w; ++j)
//			cout << v[i][j] << (" \n"[j+1==w]);
}