#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int H, W, K;
char c[810][810];
int sx, sy;
int d[810][810];

int main() {
	scanf("%d %d %d", &H, &W, &K);
	memset(d, -1, sizeof(d));
	queue<pii> que;

	rep(i, H) {
		scanf("%s", c[i]);
		rep(j, W) {
			if (c[i][j] == 'S') {
				sy = i;
				sx = j;
				c[i][j] = '.';
				que.push(mp(i, j));
				d[i][j] = 0;
			}
		}
	}

	int ret = 100000;

	while (!que.empty()) {
		pii p = que.front(); que.pop();
		int y = p.fi, x = p.se;
		if (d[y][x] > K) break;

		int t = min({y, x, H-1-y, W-1-x});
		ret = min(ret, (t + K  - 1) / K + 1);

		rep(i, 4) {
			int ny = y + dy[i], nx = x + dx[i];
			if (ny >= 0 && ny < H && nx >= 0 && nx < W && c[ny][nx] == '.' && d[ny][nx] == -1) {
				d[ny][nx] = d[y][x] + 1;
				que.push(mp(ny, nx));
			}
		}
	}

	cout << ret << endl;

	return 0;
}