#include<bits/stdc++.h>
#define randome_generate srand(std::chrono::high_resolution_clock::now().time_since_epoch().count())
using namespace std;
char grid[1000][1000];
int n,m,k;
int dist[1000][1000];
queue<pair<int,int> >q;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n>>m>>k;
	memset(dist,63,sizeof(dist));
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>grid[i][j];
			if(grid[i][j]=='S'){
				q.push(make_pair(i,j));
				dist[i][j]=0;
			}
		}
	}
	int ans=2e9,res;
	while(q.size()){
		int x=q.front().first,y=q.front().second;
		q.pop();
		ans=min(ans,min(min(x-1,n-x),min(y-1,m-y)));
		for(int i=0;i<4;i++){
			int nx=x+dx[i],ny=y+dy[i];
			if(nx>=1&&nx<=n&&ny>=1&&ny<=m){
				if(grid[nx][ny]=='.'){
					if(dist[nx][ny]>dist[x][y]+1){
						dist[nx][ny]=dist[x][y]+1;
						if(dist[nx][ny]<=k)
							q.push(make_pair(nx,ny));
					}
				}
			}
		}
	}
	res=(ans+k-1)/k+1;
	cout<<res<<endl;
	return 0;
}
