#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int,int> mp;
const int maxn = 805;
const int inf = 1<<30;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

char s[maxn][maxn];
int g[maxn][maxn],h,w,k,cx,cy,ans=inf;
queue<mp> q;
bool inG(int x,int y) {
	return x>=1&&y>=1&&x<=h&&y<=w&&s[x][y]!='#';
}

int main()
{
	#ifdef Amberframe
		freopen("agc014c.in","r",stdin);
		freopen("agc014c.out","w",stdout);
	#endif
	scanf("%d %d %d",&h,&w,&k);
	for (int i=1;i<=h;i++) scanf("%s",s[i]+1);
	for (int i=1;i<=h;i++)
	for (int j=1;j<=w;j++) g[i][j]=inf;
	for (int i=1;i<=h;i++)
	for (int j=1;j<=w;j++)
		if (s[i][j]=='S') cx=i,cy=j;
	
	g[cx][cy]=0;q.push(mp(cx,cy));
	while (!q.empty()) {
		int x=q.front().first;
		int y=q.front().second;
		q.pop();
		if (g[x][y]>=k) break;
		for (int nx,ny,d=0;d<4;d++)
		if (inG(nx=x+dx[d],ny=y+dy[d])&&g[nx][ny]==inf)
			g[nx][ny]=g[x][y]+1,q.push(mp(nx,ny));
	}
	for (int i=1;i<=h;i++)
	for (int j=1;j<=w;j++)
		if (g[i][j]<=k) ans=min(ans,min(min(i-1,h-i),min(j-1,w-j)));
	printf("%d",ans?(ans-1)/k+2:1);
	return 0;
}