#include <bits/stdc++.h>
using namespace std;
long long h, w, k;
vector<string> a;
long long sx, sy;
long long mind = 10000000000;
long long near(long long x, long long y) {
  return min({x, y, h - x - 1, w - y - 1});
}
vector<long long> par;
vector<long long> rnk;
void init(long long n) {
  par = vector<long long>(n);
  rnk = vector<long long>(n);
  for (long long i = 0; i < n; i++) par[i] = i;
}
long long find(long long x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = find(par[x]);
}
void unite(long long x, long long y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (rnk[x] < rnk[y])
    par[x] = y;
  else {
    par[y] = x;
    if (rnk[x] == rnk[y]) rnk[x]++;
  }
}
bool same(long long x, long long y) { return find(x) == find(y); }
bool isin(long long i, long long j) {
  return i >= 0 && j >= 0 && i < h && j < w && a[i][j] != '#';
}
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
  cin >> h >> w >> k;
  a = vector<string>(h);
  init(h * w);
  for (long long i = 0; i < h; i++) cin >> a[i];
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (a[i][j] != '#') {
        for (long long k = 0; k < 4; k++) {
          long long x = i + dx[k];
          long long y = j + dy[k];
          if (isin(x, y)) {
            unite(i * w + j, x * w + y);
          }
        }
      }
    }
  }
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (same(i * w + j, sx * w + sy) && i - sx + j - sy <= k) {
        mind = min(mind, near(i, j));
      }
    }
  }
  if (mind == 0)
    cout << 1 << endl;
  else
    cout << (mind - 1) / k + 2 << endl;
  return 0;
}
