#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};

int dp[h][w][801];
bool used[h][w];

int main(){
	int h, w, K; cin >> h >> w >> K; std::vector<std::string> s(h);
	for(int i = 0; i < h; i++) cin >> s[i];
	
	int sx, sy;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(s[i][j] != 'S') continue;
			sx = i;
			sy = j;
			i = j = 1e9;
		}
	}
	
	used[sx][sy] = true;
	typedef std::pair<int, std::pair<int, int>> P;
	std::queue<P> qu; qu.push({0, {sx, sy}});
	for(int i = 0; i < h; i++)
		for(int j = 0; j < w; j++)
			for(int k = 0; k < 801; k++)
				dp[i][j][k] = 1e9;
	
	dp[sx][sy][0] = 0;
	while(!qu.empty()){
		auto p = qu.front(); qu.pop();
		
		for(int i = 0; i < 4; i++){
			int nx = p.second.first + vy[i], ny = p.second.second + vx[i];
			if(nx<0||ny<0||nx>=h||ny>=w) continue;
			int block = (s[nx][ny] == '#');
			
			if(dp[p.second.first][p.second.second][p.first] + 1 >= dp[nx][ny][p.first + block]) continue;
			dp[nx][ny][p.first + block] = dp[p.second.first][p.second.second][p.first] + 1;
			qu.push({p.first + block, {nx, ny}});
		}
	}
	
	int mi = 1e9;
	for(int i = 0; i < w; i++){
		for(int j = 1; j < 801; j++){
			mi = std::min(mi, std::max(std::max(2, (dp[0][i][j] + K - 1) / K), j / K + 1));
			mi = std::min(mi, std::max(std::max(2, (dp[i][0][j] + K - 1) / K), j / K + 1));
			mi = std::min(mi, std::max(std::max(2, (dp[h - 1][i][j] + K - 1) / K), j / K + 1));
			mi = std::min(mi, std::max(std::max(2, (dp[i][w - 1][j] + K - 1) / K), j / K + 1));
		}
		
		mi = std::min(mi, (dp[0][i][0] + K - 1) / K);
		mi = std::min(mi, (dp[i][0][0] + K - 1) / K);
		mi = std::min(mi, (dp[h - 1][i][0] + K - 1) / K);
		mi = std::min(mi, (dp[i][w - 1][0] + K - 1) / K);
	}
	
	
	cout << mi << endl;
	return 0;
}
