#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
#include<iomanip>

using namespace std;

class A {
public:
	int x, y, k, w, h, kk;
	A(){}
	bool operator<(A &rhs) {
		return this->k > rhs.k;
	}
	bool operator>(A &rhs) {
		return this->k < rhs.k;
	}
};

int ans = 0;
vector<string> mp;
vector<vector<int>> b;
vector<A> a;

int check(int x, int y, int k, int w, int h) {
	//cout << x << " " << y << endl;
	return ceil(double(min(min(abs(x - w) - 1, x), min(abs(y - h)-1, y))) / k) + 1;
}

void DFS() {
	int x = a[0].x;
	int y = a[0].y;
	int k = a[0].k;
	int w = a[0].w;
	int h = a[0].h;
	int kk = a[0].kk;
	a.erase(a.begin());
	if (x == 0 || x == w - 1 || y == 0 || y == h - 1) {
		ans = 1;
		return;
	}
	if (b[y][x] > k) {
		return;
	}
	b[y][x] = k;
	if (ans > check(x, y, kk, w, h)) {
		ans = check(x, y, kk, w, h);
	}
	if (k < 0) {
		return;
	}
	if (mp[y][x - 1] == '.') {
		if (x - 1 <= 0) {
			ans = 1;
			return;
		}
		if (b[y][x-1] > k - 1) {
			return;
		}
		A hoge;
		hoge.x = x-1;
		hoge.y = y;
		hoge.k = k-1;
		hoge.w = w;
		hoge.h = h;
		hoge.kk = kk;
		a.push_back(hoge);
	}
	if (mp[y][x + 1] == '.') {
		if (x + 1 >= w - 1) {
			ans = 1;
			return;
		}
		if (b[y][x+1] > k - 1) {
			return;
		}
		A hoge;
		hoge.x = x+1;
		hoge.y = y;
		hoge.k = k-1;
		hoge.w = w;
		hoge.h = h;
		hoge.kk = kk;
		a.push_back(hoge);
	}
	if (mp[y - 1][x] == '.') {
		if (b[y-1][x] > k - 1) {
			return;
		}
		if (y - 1 <= 0) {
			ans = 1;
			return;
		}
		A hoge;
		hoge.x = x;
		hoge.y = y-1;
		hoge.k = k-1;
		hoge.w = w;
		hoge.h = h;
		hoge.kk = kk;
		a.push_back(hoge);
	}
	if (mp[y + 1][x] == '.') {
		if (y + 1 >= h - 1) {
			ans = 1;
			return;
		}
		if (b[y+1][x] > k - 1) {
			return;
		}
		A hoge;
		hoge.x = x;
		hoge.y = y+1;
		hoge.k = k-1;
		hoge.w = w;
		hoge.h = h;
		hoge.kk = kk;
		a.push_back(hoge);
	}
}

int main() {
	int h, w, k;
	cin >> h >> w >> k;
	mp.resize(h);
	b.resize(h);
	for (int i = 0; i < h; i++) {
		cin >> mp[i];
		b[i].resize(w);
		for (int j = 0; j < w; j++)
			b[i][j] = 0;
	}
	int x, y;
	for (int i = 0; i < h; i++)
		for (int j = 0; j < w; j++)
			if (mp[i][j] == 'S') {
				x = j;
				y = i;
			}

	ans = w*h;
	A hoge;
	hoge.x = x;
	hoge.y = y;
	hoge.k = k;
	hoge.w = w;
	hoge.h = h;
	hoge.kk = k;
	a.push_back(hoge);
	int K = k;
	while(true) {
		if(a.size() > 0) {
			if(K > a[0].k) {
				sort(a.begin(), a.end());
				K = a[0].k;
			}
			DFS();
		}
		else {
			break;
		}
	}
	cout << ans << endl;
	//cin >> k;
	return 0;
}
