#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];
int mn = INF;

bool ok( int n ) {
  if( mn - (n-1) * K <= 0 ) return true;
  else return false;
}

void bfs()
{
  queue<Pip> q;
  bool used[801][801] = {{0}};
  q.push( Pip(K, Pii(sy, sx)) );


  while( !q.empty() ) {
    Pip p = q.front(); q.pop();
    int k = p.fr, y = p.sc.fr, x = p.sc.sc;
    //cout << y << " " << x << endl;
    mn = min( mn, min(y - 0, H-1 - y ) );
    mn = min( mn, min(x - 0, W-1 - x ) );

    if( used[y][x] ) continue;
    used[y][x] = true;

    for(int i=0; i<4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if( nx < 0 || ny < 0 || nx >= W || ny >= H || data[ny][nx] == '#' ) continue;
      q.push( Pip(k-1, Pii(ny, nx)) );
    }
  }
}

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] =  '.';
    }
  }

  bfs();
  //cout << mn << endl;
  for(int i=1; i<1000; i++) {
    if( ok( i ) ) {
      cout << i << endl;
      break;
    }
  }
}
