#include <bits/stdc++.h>
using std::cout;
using std::endl;
using std::cin;
constexpr int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};

std::vector<std::string> s;
int h, w, k, sx, sy;

int dist(int x, int y){
	int A = std::min({abs(x - (h - 1)), x, abs(y - (w - 1)), y});
	assert(A >= 0);
	
	return (A + k - 1) / k;
}

int main(){
	cin >> h >> w >> k; s.resize(h);
	for(int i = 0; i < h; i++){
		cin >> s[i];
		
		for(int j = 0; j < w; j++){
			if(s[i][j] != 'S') continue;
			sx = i; sy = j;
			break;
		}
	}
	
	std::queue<std::pair<int, int>> qu; qu.push({sx, sy});
	std::vector<std::vector<int>> min_cost(h, std::vector<int>(w, 1e9));
	min_cost[sx][sy] = 0;
	while(!qu.empty()){
		auto p = qu.front(); qu.pop();
		
		for(int i = 0; i < 4; i++){
			int nx = p.first + vy[i], ny = p.second + vx[i];
			if(nx<0||ny<0||nx>=h||ny>=w||s[nx][ny] == '#') continue;
			if(min_cost[p.first][p.second] + 1 >= min_cost[nx][ny]) continue;
			min_cost[nx][ny] = min_cost[p.first][p.second] + 1;
			qu.push({nx, ny});
		}
	}
	
	int ans = 1e9;
	for(int i = 0; i < h; i++)
		for(int j = 0; j < w; j++)
			if(min_cost[i][j] <= k) ans = std::min(ans, dist(i, j) + 1);
			
	cout << ans << endl;
	return 0;
}
