#include<bits/stdc++.h>
using namespace std;
#define int long long
int dp[808][808][444];
struct st{
  int y,x,d;
  st(){}
  st(int y,int x,int d):y(y),x(x),d(d){}
};
signed main(){
  int h,w,k;
  cin>>h>>w>>k;
  string a[h];
  for(int i=0;i<h;i++) cin>>a[i];
  int sy,sx;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(a[i][j]=='S') sy=i,sx=j;
    }
  }
  memset(dp,-1,sizeof(dp));
  queue<st> q;
  q.emplace(sy,sx,0);
  dp[sy][sx][0]=0;
  int ans=8000;
  int ay[]={0,0,1,-1,0};
  int ax[]={1,-1,0,0,0};
  while(!q.empty()){
    st p=q.front();q.pop();
    int y=p.y,x=p.x,d=p.d;
    //cout<<y<<" "<<x<<" "<<d<<":"<<dp[y][x][d]<<endl;
    for(int i=0;i<5;i++){
      int ny=y+ay[i],nx=x+ax[i];
      if(ny<0||ny>=h||nx<0||nx>=w){
	ans=min(ans,(dp[y][x][d]-1)/k+1);
	goto END;
      }
      int nd=d+(a[ny][nx]=='#');
      if(nd>440) continue;
      //if(~dp[ny][nx][nd]) continue;
      if(dp[y][x][d]/k*k<nd) continue;
      dp[ny][nx][nd]=dp[y][x][d]+1;
      q.emplace(ny,nx,nd);
    }
  }
 END:
  cout<<ans<<endl;
  return 0;
}
