#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TP = tuple<ll, ll, ll>;
void init_io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
signed main() {
  init_io();
  ll h, w, k, ans = 1e13;
  ll sx = 0, sy = 0;
  ll mv = 1e9;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (int i = 0; i < h; i++) {
    cin >> a[i];
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
        a[i][j] = '.';
      }
    }
  }
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      if (abs(i - sx) + abs(j - sy) > k) continue;
      if (a[i][j] == '#') {
        continue;
      }
      mv = min(mv, min(i, h - 1 - i));
      mv = min(mv, min(j, w - 1 - j));
    }
  }
  ans = 1 + (mv + k - 1) / k;
  cout << ans << endl;
}
