#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class... Args>
void dump(Args&&... args) {}
signed main() {
  ll H, W, K;
  cin >> H >> W >> K;
  ll sr, sc;
  vector<vector<bool>> m(H, vector<bool>(W));
  for (ll i = (ll)(0); i < (ll)((H)); i++) {
    string s;
    cin >> s;
    for (ll j = (ll)(0); j < (ll)((W)); j++) {
      m[i][j] = s[j] != '#';
      if (s[j] == 'S') {
        sr = i;
        sc = j;
      }
    }
  }
  vector<vector<ll>> dist(H, vector<ll>(W, 1e9));
  dist[sr][sc] = 0;
  ll dr[] = {-1, 0, 1, 0};
  ll dc[] = {0, 1, 0, -1};
  queue<pair<ll, ll>> q;
  q.emplace(sr, sc);
  while (q.size()) {
    ll r = q.front().first;
    ll c = q.front().second;
    q.pop();
    ll d = dist[r][c];
    for (ll i = (ll)(0); i < (ll)((4)); i++) {
      ll nr = r + dr[i];
      ll nc = c + dc[i];
      if (!((((0)) <= ((nr)) && ((nr)) < ((H))) &&
            (((0)) <= ((nc)) && ((nc)) < ((W)))))
        continue;
      if (!m[nr][nc] || dist[nr][nc] <= d + 1) continue;
      dist[nr][nc] = d + 1;
      q.emplace(nr, nc);
    }
  }
  ll res = 1e9;
  for (ll i = (ll)(0); i < (ll)((H)); i++) {
    for (ll j = (ll)(0); j < (ll)((W)); j++) {
      if (dist[i][j] == 1e9) continue;
      ll h = min(i, H - 1 - i);
      ll w = min(j, W - 1 - j);
      res = min(res, (1 + (min(h, w) + K - 1) / K));
    }
  }
  cout << res << endl;
  return 0;
}
