#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back 
#define pf push_front 
#define mp make_pair
#define fr first
#define sc second
#define Rep(i,n) for(int i=0;i<(n);i++)
#define All(v) v.begin(),v.end()
typedef pair<int, int> Pii; typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;

int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

int H, W, K;
int sx, sy;
char data[801][801];

bool ok( int n ) {
  int mx = INF;
  int nx = sx, ny = sy;
  //cout << sx << sy << endl;
  while( 1 ) {
    if( ny == 0 ) break;
    if( data[nx][ny-1] == '#' ) break;
    ny--;
  }
  
  mx = min(mx, ny - 0);

  ny = sy; nx = sx;
  while( 1 ) {
    if( ny == H-1 ) break;
    if( data[nx][ny+1] == '#' ) break;
    ny++;
  }
  
  mx = min(mx, H-1 - ny);

  nx = sx; ny = sy;
  while( 1 ) {
    if( nx == W-1 ) break;
    if( data[nx+1][ny] == '#' ) break;
    nx++;
  }
  
  mx = min(mx, W-1 - nx);

  nx = sx; ny = sy;
  while( 1 ) {
    if( nx == 0 ) break;
    if( data[nx-1][ny] == '#' ) break;
    nx--;
  }
  
  mx = min(mx, nx - 0);

  //cout << mx << endl;
  if( mx - (n-1) * K <= 0 ) return true;
  else return false;
}

main()
{
  cin >> H >> W >> K;

  Rep(i, H) Rep(j, W) {
    cin >> data[i][j];
    if( data[i][j] == 'S' ) {
      sy = i; sx = j;
      data[i][j] =  '.';
    }
  }

  int l = 0, r = 1000;
  while( r - l > 1 ) {
    int mid = (l + r) / 2;
    if( ok( mid ) ) r = mid;
    else l = mid;
  }

  if( ok( l ) )  cout << l << endl;
  else cout << r << endl;
}
