#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using ld = long double;

template <typename T>
int sz(T &x) {
  return (int)(x.size());
}

int power(int x, int y, int mod) {
  if (y == 0) {
    return 1;
  }
  int z = power(x, y >> 1, mod);
  z = z * 1ll * z % mod;
  if (y & 1) {
    z = z * 1ll * x % mod;
  }
  return z;
}

const int mod = 998244353;
const int inf = 1'000'000'007;

void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<vector<char> > a(n + 2, vector<char>(m + 2, '#'));

  int si(0), sj(0);

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }

  vector<vector<int> >dist(n + 2, vector<int> (m + 2, inf));

  queue<pii> f;
  f.push(make_pair(si, sj));
  dist[si][sj] = 0;
  while (!f.empty()) {
    pii v = f.front();
    f.pop();
    for (int dx : {-1, 0, 1}) {
      for (int dy : {-1, 0, 1}) {
        if ((dx == 0) + (dy == 0) != 1) {
          continue;
        }
        int cx = v.first + dx;
        int cy = v.second + dy;
        if (dist[cx][cy] != inf || a[cx][cy] == '#') {
          continue;
        }
        dist[cx][cy] = dist[si][sj] + 1;
        f.push(make_pair(cx, cy));
      }
    }
  }
  int ans = inf;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (dist[i][j] <= k) {
        ans = min(ans, 1 + (min({i - 1, j - 1, n - i, m - j}) + k - 1) / k);

      }
    }
  }
  cout << ans << "\n";
}

int main() {
  #ifndef LOCAL58
    // don't forget to remove if interactive
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);
  #endif

  int t;
  t = 1;
  while (t--) {
    solve();
  }

  return 0;
}
