#include<bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
const int N = 810;
int h, w, k, ans, sx, sy;
int d[N][N], q[N * N * 2];
char Map[N][N];
int main()
{
	scanf("%d%d%d", &h, &w, &k);
	for(int i = 0; i <= h + 1; ++i)
		for(int j = 0; j <= w + 1; ++j) Map[i][j] = '#';
	for(int i = 1; i <= h; ++i)
	{
		char s[N];
		scanf("%s", s + 1);
		for(int j = 1; j <= strlen(s + 1); ++j) 
		{
			if(s[j] == 'S')
			{
				sx = i;
				sy = j;
			}
			Map[i][j] = s[j];
		}
	}
	ans = 1 << 29;
	int l = 1, r = 0;
	q[++r] = sx;
	q[++r] = sy;
	while(l <= r)
	{
		int x = q[l++], y = q[l++];
		ans = min(ans, min(min(x - 1, h - x), min(y - 1, w - y)));
		if(d[x][y] == k) continue;
		for(int i = 0; i < 4; ++i)
		{
			int xx = x + dx[i], yy = y + dy[i];
			if(!d[xx][yy] && Map[xx][yy] == '.') 
			{
				q[++r] = xx;
				q[++r] = yy;
				d[xx][yy] = d[x][y] + 1;
			}
		}
	}
	ans = (ans + k - 1) / k + 1;
	printf("%d\n", ans);		
	return 0;
}