#include <bits/stdc++.h>
using namespace std;
const int di[4] = {-1, 0, 1, 0}, dj[4] = {0, 1, 0, -1}, MAX = 800, INF = 1e9;

int n, m, k, ans = INF, d[MAX][MAX];
string s[MAX];
pair<int,int> startPos;
inline bool valid(int i, int j) {
	return i >= 0 && i < n && j >= 0 && j < m;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) d[i][j] = INF;
	for(int i = 0; i < n; i++) {
		cin >> s[i];
		for(int j = 0; j < m; j++) if(s[i][j] == 'S') startPos = {i, j};
	}
	queue<pair<int,int> > q;
	q.push(startPos);
	d[startPos.first][startPos.second] = 0;
	while(!q.empty()) {
		int i, j;
		tie(i, j) = q.front();
		q.pop();
		for(int z = 0; z < 4; z++) {
			if(valid(i+di[z], j+dj[z]) and d[i+di[z]][j+dj[z]] == INF and s[i+di[z]][j+dj[z]] != '#'){
				q.emplace(i+di[z], j+dj[z]);
				d[i+di[z]][j+dj[z]] = d[i][j]+1;
			}
		}
	}
	for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(d[i][j] <= k) {
		ans = min((min(min(i, n-1-i), min(j, m-1-j))+k-1)/k+1, ans);
	}
	cout << ans;
}
