#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
const int nmax=805;
int d1[]={-1,0,1,0};
int d2[]={0,-1,0,1};
pair<int,int> q[nmax*nmax];
string s[nmax];
int d[nmax][nmax];
int n,m,k,i,j,l1,c1,p,u,li,ci,lf,cf,ans;
int main()
{
    cin>>n>>m>>k;
    for(i=1;i<=n;i++)
    {
        cin>>s[i];
        s[i]=" "+s[i];
        for(j=1;j<=m;j++)
            if(s[i][j]=='S')
        {
            l1=i;c1=j;
        }
        else
            if(s[i][j]=='#')
               d[i][j]=-1;
    }
    q[u=1]={l1,c1};
    d[l1][c1]=1;
    for(p=1;p<=u;p++)
    {
        li=q[p].first;ci=q[p].second;
        for(i=0;i<4;i++)
        {
            lf=li+d1[i];cf=ci+d2[i];
            if(lf>=1&&lf<=n&&cf>=1&&cf<=m&&(!d[lf][cf]))
            {
                q[++u]={lf,cf};
                d[lf][cf]=d[li][ci]+1;
            }
        }
    }
    ans=n*m+1;
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
          if(d[i][j]>=1&&d[i][j]<=k+1)
          {
              ans=min(ans,(n-i+k-1)/k+1);
              ans=min(ans,(i-1+k-1)/k+1);
              ans=min(ans,(m-j+k-1)/k+1);
              ans=min(ans,(j-1+k-1)/k+1);
          }
    cout<<ans;
    return 0;
}
