#include <bits/stdc++.h>

#define MAXH (810)

int h, w, k, sx, sy;
int ans, dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char str[MAXH][MAXH];
int dis[MAXH][MAXH];

void bfs(){
	std::queue<std::pair<int, int> > queue;
	memset(dis, -1, sizeof(dis));
	dis[sx][sy] = 0;
	queue.push({sx, sy});
	while(!queue.empty()){
		int x = queue.front().first, y = queue.front().second;
		queue.pop();
		for(int i = 0; i < 4; ++ i){
			int dx = x + dir[i][0], dy = y + dir[i][1];
			if(dx < 1 || dx > h || dy < 1 || dy > w || ~dis[dx][dy] || str[dx][dy] == '#') continue;
			dis[dx][dy] = dis[x][y] + 1;
			if(dis[dx][dy] <= k){
				queue.push({dx, dy});
				ans = std::min(ans, (std::min(std::min(dx - 1, h - dx), std::min(dy - 1, w - dy)) + k - 1) / k + 1);
			}
		}
	}
}

int main(){
	scanf("%d%d%d", &h, &w, &k);
	for(int i = 1; i <= h; ++ i){
		scanf("%s", str[i] + 1);
		for(int j = 1; j <= w; ++ j){
			if(str[i][j] == 'S'){
				sx = i;
				sy = j;
			}
		}
	}
	ans = (std::min(std::min(sx - 1, h - sx), std::min(sy - 1, w - sy)) + k - 1) / k + 1;
	bfs();
	printf("%d\n", ans);
	return 0;
}