#include <bits/stdc++.h>
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = acos(-1);
using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;
ll dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
ll dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
signed main() {
  ll H, W, K;
  cin >> H >> W >> K;
  vector<string> s(H);
  for (ll i = 0; i < ll(H); ++i) cin >> s[i];
  ll sx, sy;
  for (ll i = 0; i < ll(H); ++i)
    for (ll j = 0; j < ll(W); ++j) {
      if (s[i][j] == 'S') tie(sx, sy) = {i, j};
    }
  queue<pair<ll, ll>> q;
  q.push({sx, sy});
  vector<vector<ll>> dist(H, vector<ll>(W, INF));
  dist[sx][sy] = 0;
  ll MIN = INF;
  while (!q.empty()) {
    ll x = q.front().first;
    ll y = q.front().second;
    q.pop();
    chmin(MIN, min(min(x, H - x), min(y, W - y)));
    if (dist[x][y] > K) break;
    for (ll i = 0; i < ll(4); ++i) {
      ll xx = x + dx[i];
      ll yy = y + dy[i];
      if ((xx < 0 || H <= xx)) continue;
      if ((yy < 0 || W <= yy)) continue;
      if (s[xx][yy] == '#') continue;
      if (dist[xx][yy] > dist[x][y] + 1) {
        dist[xx][yy] = dist[x][y] + 1;
        q.push({xx, yy});
      }
    }
  }
  cout << 1 + (MIN + K - 1) / K << "\n";
  return 0;
}
