#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#define N 810

using namespace std;

char a[N][N];
int dis[N][N];
int n,m,k,bx,by,Min,Ans;

void dfs(int x,int y,int s){
  if(s>k) return ;
  if(x==1||x==n||y==1||y==m) { puts("1"); exit(0); }
  if(dis[x][y]<=s) return ;
  dis[x][y]=s;
  int now=min(min(x-1,n-x),min(y-1,m-y));
  if(now<Min) Min=now;
  if(a[x+1][y]!='#') dfs(x+1,y,s+1);
  if(a[x-1][y]!='#') dfs(x-1,y,s+1);
  if(a[x][y+1]!='#') dfs(x,y+1,s+1);
  if(a[x][y-1]!='#') dfs(x,y-1,s+1);
}

inline int Ceil(int x,int y){
  if(x%y) return x/y+1;
  else return x/y;
}

int main(){
  scanf("%d%d%d",&n,&m,&k);
  for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
  for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
      if(a[i][j]=='S') bx=i,by=j;
  memset(dis,0x7f,sizeof(dis));
  Min=min(min(bx-1,n-bx),min(by-1,m-by));
  dfs(bx,by,0);
  Ans=1+Ceil(Min,k);
  printf("%d\n",Ans);
  return 0;
}
