#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n,m,k,x,y,dis[N][N];
char s[N][N];
typedef pair<int,int> pii;
queue<pii> Q;
const int fx[4]={1,0,-1,0},fy[4]={0,1,0,-1};
int main()
{
scanf("%d%d%d",&n,&m,&k);
for (int i=1;i<=n;i++) scanf("%s",s[i]+1);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (s[i][j]=='S') x=i,y=j,s[i][j]='.';
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
dis[i][j]=1e9;
dis[x][y]=0;
int ans=1e9;
Q.push(pii(x,y));
while (!Q.empty()){
pii now=Q.front();Q.pop();
int x=now.first,y=now.second;
ans=min(ans,x-1);
ans=min(ans,y-1);
ans=min(ans,n-x);
ans=min(ans,m-y);
if (dis[x][y]==k) continue;
for (int i=0;i<4;i++){
int a=x+fx[i],b=y+fy[i];
if (s[a][b]!='.') continue;
if (dis[a][b]>dis[x][y]+1) dis[a][b]=dis[x][y]+1,Q.push(pii(a,b));
}
}
printf("%d\n",(ans+k-1)/k+1);
return 0;
}