#include<bits/stdc++.h>
using namespace std;
const int move[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
#define INF 0x3f3f3f3f
template<typename T>
void read(T &x)
{
    char ch=getchar();
    int f=1;
    for (x=0;ch!='-'&&(ch<'0'||ch>'9');ch=getchar());
    if (ch=='-') f=-1,ch=getchar();
    for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
    x*=f;
}
int n,m,k,ans,x,y;
char mp[810][810];
bool vis[810][810];
char getopt()
{
	char ch=getchar();
	while (ch!='#'&&ch!='.'&&ch!='S') ch=getchar();
	return ch;
}
int calc(int x,int y)
{
	return min(min(x-1,n-x),min(y-1,m-y))/k;
}
void dfs(int x,int y,int deep)
{
	vis[x][y]=1;
	ans=min(ans,calc(x,y));
	for (int i=0;i<4;i++)
	{
		int newx=x+move[i][0];
		int newy=y+move[i][1];
		if (deep<k&&newx>=1&&newx<=n&&newy>=1&&newy<=m&&mp[newx][newy]=='.'&&!vis[newx][newy])
			dfs(newx,newy,deep+1);
	}
}
int main()
{
	read(n),read(m),read(k);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
		{
			mp[i][j]=getopt();
			if (mp[i][j]=='S')
				x=i,y=j;
		}
	ans=INF;
	dfs(x,y,0);
	printf("%d",ans+1);
	return 0;
} 