#include <iostream>
#include <queue>
#define inf 1000000000

using namespace std;

int H, W, K;
char map[805][805];
int sx, sy;
int dist[805][805];
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

int bfs()
{
	for(int x = 0; x < W; x++){
		for(int y = 0; y < H; y++){
			dist[x][y] = inf;
		}
	}
	dist[sx][sy] = 0;
	
	int ret = inf, d;
	queue<int> Q;
	Q.push(sx*1000+sy);
	int x, y, nx, ny;
	while(Q.size()){
		x = Q.front() / 1000;
		y = Q.front() % 1000;
		Q.pop();
		d = min(x, y);
		d = min(d, W-x-1);
		d = min(d, H-y-1);
		ret = min(ret, d);
		if(dist[x][y] == K) continue;
		for(int d = 0; d < 4; d++){
			nx = x + dx[d], ny = y + dy[d];
			if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
			if(map[nx][ny] == '#') continue;
			if(dist[nx][ny] != inf) continue;
			Q.push(nx*1000+ny);
			dist[nx][ny] = dist[x][y] + 1;
		}
	}
	return ret;
}

int main(void)
{
	cin >> H >> W >> K;
	for(int y = 0; y < H; y++){
		for(int x = 0; x < W; x++){
			cin >> map[x][y];
			if(map[x][y] == 'S'){
				sx = x, sy = y;
				map[x][y] = '.';
			}
		}
	}
	
	int ans = bfs();
	cout << (ans+K-1) / K + 1<< endl;
	return 0;
}