#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

bool inside(int x, int y, int xlim, int ylim){
	return 0 <= x && x < xlim && 0 <= y && y < ylim;
}

int main(){
	constexpr int dd[] = {0, 1, 0, -1, 0};
	int h, w, k;
	cin >> h >> w >> k;
	string s[h];
	for(int i = 0; i < h; i++) cin >> s[i];
	queue<pair<pair<int, int>, int>> q;
	int ans = 1e9;
	for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) if(s[i][j] == 'S'){
		s[i][j] = '.';
		q.push(make_pair(make_pair(i, j), 0));
	}
	bool done[h][w]{};
	while(!q.empty()){
		auto puni = q.front();
		auto p = puni.first;
		q.pop();
		if(done[p.first][p.second] || k < puni.second) continue;
		done[p.first][p.second] = true;
		int d = min({p.first, p.second, h - p.first, w - p.second});
		ans = min(ans, (d + k - 1) / k);
		for(int i = 0; i < 4; i++){
			int nx = p.first + dd[i], ny = p.second + dd[i + 1];
			if(inside(nx, ny, h, w) && s[nx][ny] == '.'){
				q.push(make_pair(make_pair(nx, ny), puni.second + 1));
			}
		}
	}
	cout << ans + 1 << endl;
}