//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
typedef long double lb;
#define int long long
#define double long double
#define endn "\n"
typedef pair<int,int> P;
template<class T> inline bool chmin(T& a, T b){if(a>b){a = b;return 1;}return 0;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a = b;return 1;}return 0;}
const int MOD = 1000000007;
//const int MOD = 998244353;
template<class T> inline int add(T& a, T b, T M = MOD){a=(a+M)%M;b=(b+M)%M;a=(a+b)%M;return a;};
template<class T> inline int mul(T& a, T b, T M = MOD){if(a>=M)a%=M;if(b>=M)b%=M;a*=b;if(a>=M)a%=M;return a;};
const ll INF = 1e16;
const double EPS = 1e-10;
const double PI = 3.141592653589793;
const string abc="abcdefghijklmnopqrstuvwxyz";
const string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 

////////////////////////////////////////////////////////////////////
///////////////////////___modpow___////////////////////
ll modpow(ll a,  ll n, const ll &MOD = MOD){
  ll ret = n == 0 ? 1 : modpow(a, n/2, MOD);
  (ret *= ret) %= MOD;
  if(n%2)((ret *= a) %= MOD);
  return ret;
}
///////////////////////___modinv___////////////////////
ll modinv(ll d, const ll &MOD = MOD){
  return modpow(d, MOD-2, MOD);
}
////////////////////////////////////////////////////////////////////

typedef tuple<P, int> T;
int h,w,k;char a[1000][1000];
vector<P> vec;
signed main() {
  cin>>h>>w>>k;
  P s;
  rep(i,h+2)rep(j,w+2)a[i][j] = '#';
  repd(i,1,h+1)repd(j,1,w+1){
    cin>>a[i][j];
    if(a[i][j] == 'S')s = P(i,j);
  }
  queue<T> bfs;bfs.push(T(s,0));
  while(!bfs.empty()){
    T t = bfs.front();bfs.pop();
    P p = get<0>(t);int d = get<1>(t);
    if(a[p.first][p.second] == '#' || d > k)continue;
    vec.push_back(p);
    bfs.push(T(P(p.first-1, p.second), d+1));
    bfs.push(T(P(p.first+1, p.second), d+1));
    bfs.push(T(P(p.first, p.second-1), d+1));
    bfs.push(T(P(p.first, p.second+1), d+1));
  }
  int ans = INF;
  for(auto x : vec){
    chmin(ans, (min({x.first-1, x.second-1, h-x.first, w-x.second})+k-1)/k+1);
  }
  cout << ans << endl;
}

