#include<bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

char a[800][801];
int d[800][800];
int dx[]{ 1,-1,0,0 }, dy[]{ 0,0,1,-1 };
signed main() {
	int h, w, k; scanf("%d%d%d", &h, &w, &k);
	int sx, sy;
	rep(i, h) {
		scanf("%s", a[i]);
		rep(j, w) {
			if (a[i][j] == 'S')sx = i, sy = j;
		}
	}
	queue<P>que;
	memset(d, -1, sizeof(d));
	d[sx][sy] = 0; que.push(P(sx, sy));
	while (!que.empty()) {
		P p = que.front(); que.pop();
		rep(i, 4) {
			int nx = p.first + dx[i], ny = p.second + dy[i];
			if (0 <= nx&&nx < h && 0 <= ny&&ny < w&&d[nx][ny] == -1 && a[nx][ny] != '#') {
				d[nx][ny] = d[p.first][p.second] + 1;
				if (d[nx][ny] < k)que.push(P(nx, ny));
			}
		}
	}
	int Min = INF;
	rep(i, h)rep(j, w) {
		if (d[i][j] != -1) {
			int c = min({ i, h - i - 1,j, w - j - 1 });
			Min = min(Min, c / k + (c%k > 0) + 1);
		}
	}
	printf("%d\n", Min);
}