#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#define mp(x,y) make_pair(x,y)
using namespace std;

typedef pair<int,int> pi;

const int N=805;

int n,m,k,dis[N][N],sx,sy,dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
bool ma[N][N];
char str[N];
queue<pi> que;

void bfs()
{
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			dis[i][j]=k+1;
	dis[sx][sy]=0;que.push(mp(sx,sy));
	while (!que.empty())
	{
		int x=que.front().first,y=que.front().second;que.pop();
		if (dis[x][y]==k) break;
		for (int i=0;i<4;i++)
		{
			int p=x+dx[i],q=y+dy[i];
			if (p<1||p>n||q<1||q>m||ma[p][q]||dis[p][q]<=k) continue;
			dis[p][q]=dis[x][y]+1;que.push(mp(p,q));
		}
	}
}

int get(int x)
{
	return !x?0:(x-1)/k+1;
}

int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for (int i=1;i<=n;i++)
	{
		scanf("%s",str+1);
		for (int j=1;j<=m;j++)
			if (str[j]=='S') sx=i,sy=j;
			else if (str[j]=='#') ma[i][j]=1;
	}
	bfs();
	int ans=n*m;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			if (dis[i][j]<=k) ans=min(ans,min(get(i-1),min(get(j-1),min(get(n-i),get(m-j))))+1);
	printf("%d",ans);
	return 0;
}