#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long INF = 1e18 + 18;
const long long MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
istream &operator>>(istream &i, vector<T> &v) {
  for (long long j = (0); j < (v.size()); j++) i >> v[j];
  return i;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1) s << "	";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}
struct mint {
  long long x;
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < MOD);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};
void dijkstra(vector<vector<pair<long long, long long>>> g, long long start,
              vector<long long> &dist) {
  priority_queue<
      pair<long long, long long>, vector<pair<long long, long long>>,
      function<bool(pair<long long, long long>, pair<long long, long long>)>>
  q([](pair<long long, long long> a,
       pair<long long, long long> b) -> long long {
    return a.second > b.second;
  });
  dist[start] = 0;
  q.push(make_pair(start, 0));
  while (!q.empty()) {
    pair<long long, long long> now = q.top();
    q.pop();
    for (auto &nxt : g[now.first]) {
      if (chmin(dist[nxt.first], now.second + nxt.second)) {
        q.push(make_pair(nxt.first, dist[nxt.first]));
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);
  long long h, w, k;
  cin >> h >> w >> k;
  vector<string> vs(h);
  cin >> vs;
  auto idx = [&](long long y, long long x) { return y * w + x; };
  auto in = [&](long long y, long long x) {
    return y >= 0 && y < h && x >= 0 && x < w;
  };
  auto to = [&](long long id) -> pair<long long, long long> {
    return make_pair(id / w, id % w);
  };
  vector<long long> dx = {1, 0, -1, 0};
  vector<long long> dy = {0, 1, 0, -1};
  long long n = h * w;
  vector<vector<pair<long long, long long>>> vp(n);
  long long start = -1;
  for (long long i = (0); i < (h); i++) {
    for (long long j = (0); j < (w); j++) {
      if (vs[i][j] == 'S') start = idx(i, j);
      for (long long K = (0); K < (4); K++) {
        long long ny = i + dy[K];
        long long nx = j + dx[K];
        if (in(ny, nx)) {
          if (vs[ny][nx] == '.') {
            vp[idx(i, j)].emplace_back(idx(ny, nx), 1);
          } else {
            vp[idx(i, j)].emplace_back(idx(ny, nx), INF);
          }
        }
      }
    }
  }
  vector<long long> d(n, INF);
  dijkstra(vp, start, d);
  vector<vector<long long>> g(h, vector<long long>(w));
  for (long long i = (0); i < (n); i++) {
    auto [ty, tx] = to(i);
    g[ty][tx] = d[i];
  };
  long long ans = INF;
  for (long long i = (0); i < (n); i++) {
    auto [ty, tx] = to(i);
    if (d[i] <= k) {
      long long dist = INF;
      chmin(dist, tx);
      chmin(dist, w - 1 - tx);
      chmin(dist, ty);
      chmin(dist, h - 1 - ty);
      chmin(ans, (dist + k - 1) / k);
      ;
      ;
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
