#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long POWER[65];
long long power(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret *= a;
    a *= a;
    if (ret >= mod) ret %= mod;
    if (a >= mod) a %= mod;
    b >>= 1;
  }
  return ret;
}
long long inv(long long x) { return power(x, mod - 2); }
void precompute() {
  POWER[0] = 1;
  for (long long i = 1; i < 63; i++) POWER[i] = POWER[i - 1] << 1LL;
}
const long long maxn = 855;
long long sr = -1, sc = -1;
bool vis[maxn][maxn];
long long h, w, k, dis = 100000000;
vector<string> v;
queue<long long> xq, yq, le;
bool chk(long long x, long long y, long long z) {
  if (x >= 0 && x < h && y >= 0 && y < w) {
    if (vis[x][y] == 0 && v[x][y] != '#') {
      vis[x][y] = 1;
      long long d = min(min(x, h - x - 1), min(y, w - y - 1));
      dis = min(dis, d);
      if (z <= k) {
        xq.push(x);
        yq.push(y);
        le.push(z);
      }
      return 1;
    }
  }
  return 0;
}
void bfs(long long i, long long j) {
  chk(i, j, 0);
  while (!xq.empty()) {
    long long a = xq.front(), b = yq.front(), c = le.front();
    xq.pop(), yq.pop(), le.pop();
    chk(a - 1, b, c + 1);
    chk(a + 1, b, c + 1);
    chk(a, b - 1, c + 1);
    chk(a, b + 1, c + 1);
  }
}
long long main() {
std:
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> h >> w >> k;
  for (long long i = 0; i < h; i++) {
    string s;
    cin >> s;
    v.push_back(s);
  }
  for (long long i = 0; i < v.size(); i++) {
    for (long long j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 'S') {
        sr = i, sc = j;
      }
    }
  }
  bfs(sr, sc);
  long long ans = dis / k + 1;
  if (dis % k != 0) ans++;
  cout << ans << endl;
}
