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

const int N = 8e2 + 7;

char t[N][N];
int n, m, k;
int dis[N][N];
deque < pair <int, int> > d;

int main() {
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			cin >> t[i][j];
			if (t[i][j] == 'S') {
				dis[i][j] = 1;
				d.push_back({i, j});
			}
		}
	int res = n * m;
	while (d.size()) {
		int x = d.front().first, y = d.front().second;
		d.pop_front();
		if (dis[x][y] - 1 <= k) {
			res = min({res, 1 + (x - 1 + k - 1) / k, 1 + (y - 1 + k - 1) / k, 1 + (n - x + k - 1) / k, 1 + (m - y + k - 1) / k});
		}
		for (int dx = x - 1; dx <= x + 1; dx += 2) {
			if (dx >= 0 && dx <= n && t[dx][y] != '#' && !dis[dx][y])
				dis[dx][y] = dis[x][y] + 1, d.push_back({dx, y});
		}
		for (int dy = y - 1; dy <= y + 1; dy += 2) {
			if (dy >= 0 && dy <= n && t[x][dy] != '#' && !dis[x][dy]) 
				dis[x][dy] = dis[x][y] + 1, d.push_back({x, dy});
		}
	}
	cout << res;
	return 0;
}
