#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_temp>
using smaller_queue = priority_queue<Q_temp, vector<Q_temp>, greater<Q_temp> >;
const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const ll MOD = (ll)(1e9 + 7);
const double PI = acos(-1.0);
const int limit = 100010;
vector<pii> around = {make_pair(1, 0), make_pair(-1, 0), make_pair(0, 1),
                      make_pair(0, -1)};
const ll MAX_V = 801 * 801;
class dijkstra_pair {
 public:
  struct edge {
    ll to;
    ll cost;
  };

 private:
  vector<edge> E[MAX_V];
  ll d[MAX_V];
  ll H, W;

 public:
  dijkstra_pair(ll H_in, ll W_in) : H(H_in), W(W_in) {}
  void set_E(ll fi, ll fj, ll ti, ll tj, ll cost = 1) {
    E[fi * H + fj].push_back({ti * H + tj, cost});
  }
  ll get_d(ll vi, ll vj) { return d[vi * H + vj]; }
  void calc(ll si, ll sj) {
    ll s = si * H + sj;
    priority_queue<pll, vector<pll>, greater<pll> > que;
    for (ll i = 0; i < (ll)(MAX_V); ++i) d[i] = LINF;
    que.emplace(0LL, s);
    d[s] = 0LL;
    while (!que.empty()) {
      pll p = que.top();
      que.pop();
      ll v = p.second;
      if (d[v] < p.first) continue;
      for (auto&& e : E[v]) {
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          que.emplace(d[e.to], e.to);
        }
      }
    }
  }
};
inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll h, w, k;
  cin >> h >> w >> k;
  string a[h];
  for (ll i = 0; i < (ll)(h); ++i) cin >> a[i];
  dijkstra_pair ds(h, w);
  for (ll i = 0; i < (ll)(h); ++i)
    for (ll j = 0; j < (ll)(w - 1); ++j) {
      if ((a[i][j] == '.' || a[i][j] == 'S') &&
          (a[i][j + 1] == '.' || a[i][j + 1] == 'S')) {
        ds.set_E(i, j, i, j + 1);
        ds.set_E(i, j + 1, i, j);
      }
    }
  for (ll i = 0; i < (ll)(h - 1); ++i)
    for (ll j = 0; j < (ll)(w); ++j) {
      if ((a[i][j] == '.' || a[i][j] == 'S') &&
          (a[i + 1][j] == '.' || a[i + 1][j] == 'S')) {
        ds.set_E(i, j, i + 1, j);
        ds.set_E(i + 1, j, i, j);
      }
    }
  ll si, sj;
  for (ll i = 0; i < (ll)(h); ++i)
    for (ll j = 0; j < (ll)(w); ++j) {
      if (a[i][j] == 'S') {
        si = i, sj = j;
        goto loopcut;
      }
    }
loopcut:
  ds.calc(si, sj);
  ll ans = CEIL(min({si, sj, h - 1 - si, w - 1 - sj}), k) + 1;
  for (ll i = 0; i < (ll)(h); ++i)
    for (ll j = 0; j < (ll)(w); ++j) {
      if (ds.get_d(i, j) <= k) {
        ll dist = CEIL(min({i, j, h - 1 - i, w - 1 - j}), k) + 1;
        ans = min(ans, dist);
      }
    }
  for (ll i = 0; i < (ll)(h); ++i)
    for (ll j = 0; j < (ll)(w); ++j) {
      if (i != 0 && i != h - 1 && j != 0 && j != w - 1) continue;
      if (ds.get_d(i, j) <= k) ans = 1;
    }
  cout << ans << endl;
  return 0;
}
