#include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;

#define F first
#define S second

const int MAXN = 800 + 10;

int n, m, k;
string s[MAXN];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int lv[MAXN][MAXN];
pii q[MAXN*MAXN];

bool fit(int x, int y){return 0 <= x && x < n && 0 <= y && y < m && s[x][y] != '#';}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m >> k;
	for (int i = 0; i < n; i++)	cin >> s[i];
	pii a;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (s[i][j] == 'S')
				a = {i, j};

	memset(lv, 63, sizeof(lv));
	int h = 0, t = 0;
	q[t++] = a; lv[a.F][a.S] = 0;
	while (t-h){
		auto v = q[h++];
		if (lv[v.F][v.S] > k){
			h--;
			break;
		}
		for (int w = 0; w < 4; w++){
			pii u(v.F+dx[w], v.S+dy[w]);
			if (fit(u.F, u.S) && lv[v.F][v.S] + 1 < lv[u.F][u.S]){
				lv[u.F][u.S] = lv[v.F][v.S] + 1;
				q[t++] = u;
			}
		}
	}

	int ans = 1e9;
	for (int i = 0; i < h; i++){
		auto v = q[i];
		ans = min(ans, 1+(v.F+k-1)/k);
		ans = min(ans, 1+(v.S+k-1)/k);
		ans = min(ans, 1+(n-1-v.F+k-1)/k);
		ans = min(ans, 1+(m-1-v.S+k-1)/k);
	}
	cout << ans << endl;
	return 0;
}
