#include <bits/stdc++.h>

#define debug(x) cout << #x" = " << x;

#define st first
#define nd second

using namespace std;
using namespace placeholders;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

const int MAXN = 800 + 10;
const int d[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};

int n, m, K;
char s[MAXN][MAXN];
int dis[MAXN][MAXN];

int main(){
	scanf("%d%d%d", &n, &m, &K);
	int sx, sy;
	for (int i = 1; i <= n; ++i){
		scanf("%s", s[i] + 1);
		for (int j = 1; j <= m; ++j)
			if (s[i][j] == 'S')
				sx = i, sy = j;
	}

	queue<PII> Q;
	Q.emplace(sx, sy);
	dis[sx][sy] = 1;
	while (!Q.empty()){
		int x0 = Q.front().st, y0 = Q.front().nd;
		Q.pop();
		for (int i = 0; i < 4; ++i){
			int x = x0 + d[i][0], y = y0 + d[i][1];
			if (s[x][y] == '.' && !dis[x][y]){
				dis[x][y] = dis[x0][y0] + 1;
				if (dis[x][y] < K + 1)
					Q.emplace(x, y);
			}
		}
	}

	int ans = INT_MAX;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			if (dis[i][j]){
				int t = min(min(i - 1, n - i), min(j - 1, m - j));
				ans = min(ans, (t + K - 1) / K + 1);
			}
	printf("%d\n", ans);
	return 0;
}
