#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
char grid[maxn][maxn];
int dist[maxn][maxn];
int main(){
	int n, m, k; cin >> n >> m >> k;
	memset(grid, '#', sizeof(grid));
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> grid[i][j];
	memset(dist, -1, sizeof(dist));
	queue<pair<int,int> > q;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(grid[i][j] == 'S'){
		dist[i][j] = 0;
		q.push({i, j});
	}
	while(!q.empty()){
		int x, y; tie(x, y) = q.front(); q.pop();
		for(int dx = -1; dx <= 1; dx++) for(int dy = -1; dy <= 1; dy++){
			if(abs(dx) + abs(dy) == 1){
				int nx = x + dx, ny = y + dy;
				if(grid[nx][ny] != '#' && dist[nx][ny] == -1){
					dist[nx][ny] = dist[x][y] + 1;
					q.push({nx, ny});
				}
			}
		}
	}
	int ans = 1<<30;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(dist[i][j] != -1 && dist[i][j] <= k){
		ans = min(ans, 1 + (min({i - 1, n - i, j - 1, m - j}) + k - 1) / k);
	}
	cout << ans << endl;
	return 0;
}
