//author: dxm
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
inline void Fail(){
	printf("0");
	exit(0);
}

const int maxn=805;
int n,m,k,mn=INF,sx,sy;
char grid[maxn][maxn];
int dis[maxn][maxn];
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};

int Dist(int x,int y){
	return min(x,min(y,min(n-x-1,m-y-1)));
}

bool onboard(int x,int y){
	return x>=0&&x<n&&y>=0&&y<m;
} 

void bfs(){
	memset(dis,-1,sizeof(dis));
	dis[sx][sy]=0;
	queue<pii>q;
	q.push(mp(sx,sy));
	while(!q.empty()){
		int x=q.front().first,y=q.front().second;
		q.pop();
		if(dis[x][y]<=k)mn=min(mn,Dist(x,y));
		rep(i,4){
			int nx=x+dx[i],ny=y+dy[i];
			if(onboard(nx,ny)&&dis[nx][ny]==-1&&grid[nx][ny]=='.'){
				dis[nx][ny]=dis[x][y]+1;
				q.push(mp(nx,ny));
			}
		}
	}
}

int main(){
	scanf("%d%d%d",&n,&m,&k);
	rep(i,n){
		scanf("%s",grid[i]);
		rep(j,m)if(grid[i][j]=='S')sx=i,sy=j;
	}
	bfs();
	printf("%d",1+(mn+k-1)/k);
	return 0;
}

/*
Input:
7 7 1
#######
#######
##...##
#######
##.S.##
###.###
###.###
-----------------
Output:
*/