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

const int DX[] = {-1, 1, 0, 0};
const int DY[] = {0, 0, -1, 1};
const int N = 800;
bool visited[N][N];
string s[N];

int main() {
	ios::sync_with_stdio(false);
	int m, n, k; cin >> m >> n >> k;
	for (int i = 0; i < m; ++i) cin >> s[i];
	int sx = -1, sy = -1;
	for (int i = 0; i < m; ++i) {
		for (int j = 0; j < n; ++j) {
			if (s[i][j] == 'S') {
				sx = i;
				sy = j;
			}
		}
	}
	queue<pair<int, int> > q;
	q.emplace(sx, sy);
	visited[sx][sy] = true;
	for (int step = 0; step < k; ++step) {
		for (int zzz = q.size(); zzz > 0; --zzz) {
			int x = q.front().first, y = q.front().second;
			q.pop();
			for (int i = 0; i < 4; ++i) {
				int nx = x + DX[i], ny = y + DY[i];
				if (nx >= 0 && nx < m && ny >= 0 && ny < n && !visited[nx][ny] && s[nx][ny] != '#') {
					visited[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}
	int res = 1e9;
	for (int i = 0; i < m; ++i) {
		for (int j = 0; j < n; ++j) if (visited[i][j]) {
			res = min(res, min(min(i, j), min(m - i - 1, n - j - 1)));
		}
	}
	cout << (res + k - 1) / k + 1 << '\n';
	return 0;
}
