#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <cmath>

int main()
{
	long long h, w, k, sx, sy;
 
	std::cin >> h >> w >> k;
 
	std::vector<std::string> v(h);
 
	for (long long i = 0; i < h; i++) {
		std::cin >> v[i];
		auto pos = std::find(v[i].begin(), v[i].end(), 'S');
		if (pos != v[i].end()) {
			sx = i;
			sy = pos - v[i].begin();
		}
	}
 
	std::stack<std::pair<long long, long long> > s, p;
	s.push({ sx, sy });
	p.push({ sx, sy });
 
	int xx[] = { 1,0,-1,0 }, yy[] = { 0,1,0,-1 };
	while (!s.empty()) {
		int x = s.top().first, y = s.top().second;
		s.pop();
		for (int i = 0; i < 4; i++) {
			if (x + xx[i] < 0 || x + xx[i] >= h || y + yy[i] < 0 || y + yy[i] >= w) {
				continue;
			}
			if (std::abs(x + xx[i] - sx) + std::abs(y + yy[i] - sy) > k) {
				continue;
			}
			if (v[x + xx[i]][y + yy[i]] == '.') {
				s.push({ x + xx[i], y + yy[i] });
				p.push({ x + xx[i], y + yy[i] });
				v[x + xx[i]][y + yy[i]] = '#' // 一度行った場所に行かないようにする
			}
		}
	}

	long long ans = 1000000000000;
	while(!p.empty()){
		long long x = p.top().first, y = p.top().second;
		p.pop();
		ans = std::min(ans, static_cast<long long>(std::ceil(1.0 * x / k)));
		ans = std::min(ans, static_cast<long long>(std::ceil(1.0 * (h - x - 1) / k)));
		ans = std::min(ans, static_cast<long long>(std::ceil(1.0 * y / k)));
		ans = std::min(ans, static_cast<long long>(std::ceil(1.0 * (w - y - 1) / k)));
	}
 
	std::cout << ans + 1 << std::endl;
 
	return 0;
}