#include <bits/stdc++.h>

typedef std::pair<int,int>P;

int main(int argc, char *argv[])
{
  int H,W,K;
  char A[800][800];
  int d[800][800];
  P start;
  int distance=800;
  int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
  scanf("%d %d %d",&H,&W,&K);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf(" %c",&A[i][j]);
      if(A[i][j]=='S'){
	d[i][j]=0;
	start.first=i;
	start.second=j;
	distance=std::min(std::min(i,H-i-1),std::min(j,W-j-1));
      }
    }
  }
  std::queue<P> que;
  que.push(start);
  while(!que.empty()){
    P now =que.front();que.pop();
    for (int i = 0; i < 4; i++) {
      int nx=now.first+dx[i];int ny=now.second+dy[i];
      if(nx>=0&&nx<H&&ny>=0&&ny<W&&A[nx][ny]=='.'){
	P next;
	next.first=nx;next.second=ny;
	que.push(next);
	d[nx][ny]=d[now.first][now.second]+1;
	A[nx][ny]='S';
	if(d[nx][ny]<=K){
	  distance=std::min(distance,std::min(std::min(nx,H-nx-1),std::min(ny,W-ny-1)));
	}
      }
    }
  }
  if (distance==0) {
    printf("1\n");
  }else{
    printf("%d\n",1+(distance+K-1)/K);
  }
  
  return 0;
}
