#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)

char g[800][801];
int dx[4] = { 1, -1, 0, 0 }, dy[4] = { 0, 0, 1, -1 };

int main(){
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);
	f(i, 0, n)scanf("%s", g[i]);
	pair<int, int> s;
	f(i, 0, n)f(j, 0, m)if (g[i][j] == 'S') { s = make_pair(i, j); g[i][j] = 1; break; }
	queue<pair<int, pair<int, int> > > q;
	q.push(make_pair(0, s));
	while (!q.empty()){
		int a = q.front().second.first, b = q.front().second.second, c = q.front().first;
		q.pop();
		if (c == k)continue;
		f(i, 0, 4){
			int x = a + dx[i], y = b + dy[i];
			if (x == -1 || x == n || y == -1 || y == m || g[x][y] != '.')continue;
			g[x][y] = 1;
			q.push(make_pair(c + 1, make_pair(x, y)));
		}
	}
	int an = 1e9;
	f(i, 0, n)f(j, 0, m)if (g[i][j] == 1)an = min(an, min(i, min(j, min(n - (i + 1), m - (j + 1)))));
	an = (an + k - 1) / k;
	printf("%d\n", an + 1);
}