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

const int INF = 1 << 28;
int d[800][800];
string b[800];
int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };

struct S {
	int x, y;
};
int H, W, K;

bool goal(int x, int y) {
	return x == 0 || x == W - 1 || y == 0 || y == H - 1;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> H >> W >> K;

	int sx = -1, sy = -1;
	for(int i = 0; i < H; i++) {
		cin >> b[i];
		for(int j = 0; j < W; j++) {
			if(b[i][j] == 'S') sx = j, sy = i;
		}
	}

	fill((int*)begin(d), (int*)end(d), INF);
	d[sy][sx] = 0;

	int ans = INF;
	queue<S> q;
	q.push({ sx, sy });
	while(q.size()) {
		S s = q.front();
		q.pop();
		int mind = min({ s.x, s.y, W - 1 - s.x, H - 1 - s.y });
		ans = min(ans, (mind + K - 1) / K + 1);

		if(d[s.y][s.x] >= K) continue;
		for(int k = 0; k < 4; k++) {
			int nx = s.x + dx[k], ny = s.y + dy[k];
			if(b[ny][nx] == '#') continue;
			if(goal(nx, ny)) {
				cout << 1 << endl;
				return 0;
			}
			if(d[ny][nx] > d[s.y][s.x] + 1) {
				d[ny][nx] = d[s.y][s.x] + 1;
				q.push({ nx, ny });
			}
		}
	}

	cout << ans << endl;
}