#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
template <typename T>
void chmin(T &a, const T &b) {
  a = min(a, b);
}
template <typename T>
void chmax(T &a, const T &b) {
  a = max(a, b);
}
struct FastIO {
  FastIO() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fastiofastio;
const ll INF = 1LL << 60;
int main(void) {
  ll h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (ll i = (ll)0; i < (ll)h; ++i) cin >> a[i];
  ll dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
  queue<PII> que;
  vector<vector<ll>> dist(h, vector<ll>(w, INF));
  for (ll i = (ll)0; i < (ll)h; ++i)
    for (ll j = (ll)0; j < (ll)w; ++j) {
      if (a[i][j] == 'S') {
        dist[i][j] = 0;
        que.push({i, j});
      }
    }
  while (que.size()) {
    auto [y, x] = que.front();
    que.pop();
    for (ll i = (ll)0; i < (ll)4; ++i) {
      ll ny = y + dy[i], nx = x + dx[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h || a[ny][nx] == '#') continue;
      if (dist[ny][nx] == INF) {
        dist[ny][nx] = dist[y][x] + 1;
        que.push({ny, nx});
      }
    }
  };
  auto f = [&](ll len) {
    if (len == 0) return 1LL;
    return (len - 1) / k + 2;
  };
  ll ans = INF;
  for (ll i = (ll)0; i < (ll)h; ++i)
    for (ll j = (ll)0; j < (ll)w; ++j) {
      if (dist[i][j] <= k) {
        chmin(ans, f(i));
        chmin(ans, f(j));
        chmin(ans, f(h - i - 1));
        chmin(ans, f(w - j - 1));
      }
    }
  cout << ans << endl;
  return 0;
}
