#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const ll MOD = (ll)1e9 + 7;
const ll HIGHINF = (ll)1e18;
V<string> amap(800);
V<V<bool>> visited(800, V<bool>(800, false));
int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  for (ll i = 0; i < (ll)h; ++i) cin >> amap.at(i);
  ll sx, sy;
  for (ll i = 0; i < (ll)h; ++i)
    for (ll j = 0; j < (ll)w; ++j) {
      if (amap.at(i).at(j) == 'S') {
        sx = j;
        sy = i;
        break;
      }
    }
  visited.at(sy).at(sx) = true;
  queue<tuple<ll, ll, ll>> que;
  que.push(make_tuple(sx, sy, k));
  ll ans = 10000;
  while (!que.empty()) {
    tuple<ll, ll, ll> q = que.front();
    que.pop();
    if (get<2>(q) < 0)
      continue;
    else {
      ll x = get<0>(q), y = get<1>(q), rk = get<2>(q);
      ans = min(ans, min(min(x, w - x - 1), min(y, h - y - 1)));
      if (x - 1 >= 0 && amap.at(y).at(x - 1) == '.' &&
          !visited.at(y).at(x - 1)) {
        visited.at(y).at(x - 1) = true;
        que.push(make_tuple(x - 1, y, rk - 1));
      }
      if (x + 1 < w && amap.at(y).at(x + 1) == '.' &&
          !visited.at(y).at(x + 1)) {
        visited.at(y).at(x + 1) = true;
        que.push(make_tuple(x + 1, y, rk - 1));
      }
      if (y - 1 >= 0 && amap.at(y - 1).at(x) == '.' &&
          !visited.at(y - 1).at(x)) {
        visited.at(y - 1).at(x) = true;
        que.push(make_tuple(x, y - 1, rk - 1));
      }
      if (y + 1 < h && amap.at(y + 1).at(x) == '.' &&
          !visited.at(y + 1).at(x)) {
        visited.at(y + 1).at(x) = true;
        que.push(make_tuple(x, y + 1, rk - 1));
      }
    }
  }
  cout << ans / k + (1 ? ans % k : 0) + 1 << endl;
  return 0;
}
