#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <tuple>

std::pair<int, int> delta[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

int main() {
	int h, w, k, sx, sy;
	std::cin >> h >> w >> k;
	std::vector<std::string> s(h);
	for (int i = 0; i < h; i++) {
		std::cin >> s[i];
		for (int j = 0; j < w; j++) {
			if (s[i][j] == 'S') { sx = i; sy = j; }
		}
	}
	
	auto f = [&] (int i, int j) { return std::abs(i - sx) + std::abs(j - sy); };
	
	bool seen[h][w] = {};
	std::vector<std::pair<int, int>> dat;
	std::queue<std::pair<int, int>> que;
	que.emplace(sx,sy);
	
	while (not que.empty()) {
		int x, y;
		std::tie(x, y) = que.front();
		dat.emplace_back(que.front());
		que.pop();
		if (seen[x][y]) continue;
		seen[x][y] = true;
		for (std::pair<int, int> &p : delta) {
			int dx = x + p.first, dy = y + p.second;
			if (dx < 0 or h <= dx or dy < 0 or w <= dy) continue;
			if (s[dx][dy] == '#') continue;
			if (seen[dx][dy]) continue;
			if (f(sx, sy) <= k) que.emplace(dx, dy);
		}
	}
	
	int ans = (1 << 30);
	for (std::pair<int, int> p : dat) {
		int x, y; std::tie(x, y) = p;
		int min = std::min(std::min(x, y), std::min(h - 1 - x, w - 1 - y));
		min = ((min + k - 1) / k) + 1;
		ans = std::min(ans, min);
	}
	
	std::cout << ans << '\n';
	
	return 0;
}