#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
bool prime(long long x) {
  for (long long i = 2; i <= sqrt(x); i++) {
    if (x % i == 0) return false;
  }
  return true;
}
long long kai(long long x, long long y) {
  long long res = 1;
  for (long long i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= 1000000007;
  }
  return res;
}
long long mod_pow(long long x, long long y, long long m) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}
long long comb(long long x, long long y) {
  if (y > x) return 0;
  return kai(x, y) * mod_pow(kai(y, y), 1000000007 - 2, 1000000007) %
         1000000007;
}
long long h, w, k;
char a[810][810];
long long mi = 1145141919364364;
long long dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
void bfs(long long x, long long y, long long z) {
  queue<pair<long long, long long> > que;
  que.push({x, y});
  long long d[810][810];
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      d[i][j] = 1145141919364364;
    }
  }
  d[x][y] = 0;
  while (!que.empty()) {
    pair<long long, long long> p = que.front();
    que.pop();
    if (d[p.first][p.second] == z) continue;
    long long u = p.first, v = p.second;
    mi = min({mi, u, v, h - u - 1, w - v - 1});
    for (long long i = 0; i < 4; i++) {
      long long tx = u + dx[i], ty = v + dy[i];
      if (tx < 0 || h <= tx || ty < 0 || w <= ty || a[tx][ty] == '#' ||
          d[tx][ty] != 1145141919364364)
        continue;
      d[tx][ty] = d[u][v] + 1;
      que.push({tx, ty});
    }
  }
  return;
}
signed main() {
  cin >> h >> w >> k;
  long long s = 0, t = 0;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') s = i, t = j;
    }
  }
  bfs(s, t, k);
  long long ans = 1 + mi / k;
  if (mi % k) ans++;
  cout << ans << endl;
}
