#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a) for(int i=0;i<(a);i++)
const ll MOD=1000000007;

int A[808][808];
int dr[4]={-1,1,0,0};
int dc[4]={0,0,1,-1};
const int INF=1e9;

int main(){
  rep(i,808) rep(j,808) A[i][j]=INF;
  int H,W,K; cin>>H>>W>>K;
  string S[H];
  int sr,sc;
  rep(i,H){
    cin>>S[i];
    rep(j,W) if(S[i][j]=='S'){
      sr=i;
      sc=j;
    }
  }
  A[sr][sc]=0;
  queue<pair<int,int> > que;
  que.push({sr,sc});
  while(que.size()){
    pair<int,int> P=que.front(); que.pop();
    int r=P.first, c=P.second;
    rep(i,4){
      int rr=r+dr[i], cc=c+dc[i];
      if(rr>=H||rr<0||cc>=W||cc<0) continue;
      if(S[rr][cc]!='.'||A[rr][cc]!=INF) continue;
      A[rr][cc]=A[r][c]+1;
      que.push({rr,cc});
    }
  }

  int ans=1e9;
  rep(i,H) rep(j,W) if(A[i][j]<=K){
    int x=i/K+(i%K==0?0:1);
    int y=(H-1-i)/K+((H-1-i)%K==0?0:1);
    int z=j/K+(j%K==0?0:1);
    int w=(W-1-j)/K+((W-1-j)%K==0?0:1);
    ans=min({ans,x,y,z,w});
  }
  cout<<ans+1<<endl;
  return 0;
}
