#include <bits/stdc++.h>
#define mp make_pair
using namespace std;
string s[1000];
int dis[805][805];
int vis[805][805];
int main(){
	int n, m, k;
	cin >> n >> m >> k;
	int sx, sy;
	for(int i = 0; i < n; i++){
		cin >> s[i];
		for(int j = 0; j < m; j++){
			if(s[i][j] == 'S')sx = i, sy = j;
			dis[i][j] = 1e9;
			vis[i][j] = 0;
		}
	}
	int ans = 1e9;
	queue<pair<int,int> > q;
	q.push(make_pair(sx, sy));
	dis[sx][sy] = 0;
	int xx[4] = {0, 0, 1, -1};
	int yy[4] = {1, -1, 0 ,0};
	while(!q.empty()){
		int x = q.front().first, y = q.front().second;
		q.pop();
		if(vis[x][y] != 0)continue;
		if(dis[x][y] > k)continue;
		ans = min(ans, 1 + (x+k-1)/k);
		ans = min(ans, 1 + (y+k-1)/k);
		ans = min(ans, 1 + (n-x+k-2)/k);
		ans = min(ans, 1 + (m-y+k-2)/k);
		for(int i = 0; i < 4; i++){
			int xxx = x + xx[i], yyy = y + yy[i];
			if(xxx >= 0 && xxx < n && yyy >= 0 && yyy < m && s[xxx][yyy] != '#' && dis[xxx][yyy] > dis[x][y]+1 && vis[xxx][yyy] == 0){
				dis[xxx][yyy] = dis[x][y]+1;
				q.push(mp(xxx,yyy));
			}
		}
	}
	cout << ans << endl;
}