#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using V = vector<int>;
using Vll = vector<ll>;
using Vld = vector<ld>;
using Vbo = vector<bool>;
using VV = vector<V>;
using VVll = vector<Vll>;
using VVld = vector<Vld>;
using VVbo = vector<Vbo>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using Pld = pair<ld, ld>;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v) is >> e;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &e : v) os << e << " ";
  return os;
}
template <typename T>
inline int count_between(vector<T> &a, T l, T r) {
  return lower_bound(a.begin(), a.end(), r) -
         lower_bound(a.begin(), a.end(), l);
}
inline int Log2(ll x) {
  int k;
  for (k = 0; x > 0; ++k) x >>= 1;
  return k;
}
const int INF = 1 << 30;
const ll INFll = 1ll << 62;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
V dx = {1, 0, -1, 0};
V dy = {0, 1, 0, -1};
int main() {
  int h, w;
  cin >> h >> w;
  int k;
  cin >> k;
  vector<string> field(h);
  for (int i = int(0); i < int(h); ++i) cin >> field[i];
  int si, sj;
  for (int i = int(0); i < int(h); ++i)
    for (int j = int(0); j < int(w); ++j)
      if (field[i][j] == 'S') si = i, sj = j;
  VV dist(h, V(w, INF));
  dist[si][sj] = 0;
  queue<P> que;
  que.emplace(si, sj);
  while (!que.empty()) {
    int i, j;
    tie(i, j) = que.front();
    que.pop();
    for (int dir = int(0); dir < int(dx.size()); ++dir) {
      int ii = i + dx[dir];
      int jj = j + dy[dir];
      if (field[ii][jj] == '#') continue;
      if (ii < 0 || ii >= h || jj < 0 || jj >= w) continue;
      if (chmin(dist[ii][jj], dist[i][j] + 1)) {
        if (dist[ii][jj] < k) que.emplace(ii, jj);
      }
    }
  }
  ll ans = INFll;
  for (int i = int(0); i < int(h); ++i)
    for (int j = int(0); j < int(w); ++j)
      if (dist[i][j] <= k) {
        ll x = (min({i, j, h - i - 1, w - j - 1}) + k - 1) / k;
        chmin(ans, x);
      }
  ++ans;
  cout << ans << endl;
  return 0;
}
