#include<bits/stdc++.h>
using namespace std;
vector<pair<long,long>> neighbor={{0,1},{0,-1},{1,0},{-1,0}};
long H,W,K;
vector<string>A;
long escape(long y,long x){  
  return min(min((x-1)/K,(W-x-2)/K),min((y-1)/K,(H-y-2)/K))+1;
}
int main(){
  cin>>H>>W>>K;A=vector<string>(H);
  long sy,sx;
  for(long i=0;i<H;i++){
    cin>>A[i];
    if(A[i].find("S")!=string::npos){sy=i;sx=A[i].find("S");}
  }
  if(sy==0||sy==H-1||sx==0||sx==W-1){cout<<0;return 0;}
  vector<pair<long,long>>C(1,pair<long,long>(sy,sx));
  vector<vector<long>>D(H,vector<long>(W,-1));D[sy][sx]=0;
  queue<pair<long,long>>q;q.push(pair<long,long>(sy,sx));
  while(q.size()){
    pair<long,long>f=q.front();q.pop();
    for(auto n:neighbor){      
      long y=f.first+n.first,x=f.second+n.second;
      if(0<=x&&x<W&&0<=y&&y<H&&A[y][x]!='#'&&D[y][x]==-1){
        D[y][x]=D[f.first][f.second]+1;      
        if(D[y][x]<=K){
          if(y==0||y==H-1||x==0||x==W-1){cout<<1;return 0;}
          if(D[y][x]<K)q.push(pair<long,long>(y,x));    
          C.push_back(pair<long,long>(y,x));
        }
      }
    }
  }
  long ans=LONG_MAX;
  for(auto c:C)ans=min(ans,1+escape(c.first,c.second));
  cout<<ans;
}

