#include <bits/stdc++.h>
using namespace std;
const int N = 800 + 10;
int n, m, k, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, mark[N][N], dis[N][N], ans = N + 1;
string s[N];
pair<int, int> st;
bool isval(int x, int y) {
	if (x < n && x >= 0 && y < m && y >= 0 && s[x][y] == '.')
		return true;
	return false;
}
void bfs(int sx, int sy) {
	mark[sx][sy] = 1;
	queue<pair<int, int> > q;
	q.push({sx, sy});
	dis[sx][sy] = 0;
	while (!q.empty()) {
		auto p = q.front();
		q.pop();
		int x = p.first, y = p.second;
		for (int i = 0; i < 4; i++) {
			int xx = x + dx[i], yy = y + dy[i];
			if (isval(xx, yy) && !mark[xx][yy]) {
				mark[xx][yy] = 1;
				dis[xx][yy] = dis[x][y] + 1;
				q.push({xx, yy});
			}
		}
	}
}
int main() {
	cin >> n >> m >> k;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		for (int j = 0; j < m; j++)
			if (s[i][j] == 'S')
				st = {i, j};
	}
	memset(dis, 127, sizeof(dis));
	bfs(st.first, st.second);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (dis[i][j] <= k) {
				ans = min({ans, (i + k - 1) / k, (j + k - 1) / k, (n - i - 1 + k - 1) / k, (m - j - 1 + k - 1) / k});
			}	
		}
	}
	cout << ans + 1;
	return 0;
}
