#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
template <class T, class U>
void init_n(vector<T>& v, size_t n, U x) {
  v = vector<T>(n, x);
}
template <class T>
void init_n(vector<T>& v, size_t n) {
  init_n(v, n, T());
}
template <class T>
void read_n(vector<T>& v, size_t n, size_t o = 0) {
  v = vector<T>(n + o);
  for (lli i = o; i < n + o; ++i) cin >> v[i];
}
template <class T>
void read_n(T a[], size_t n, size_t o = 0) {
  for (lli i = o; i < n + o; ++i) cin >> a[i];
}
template <class T>
T gabs(const T& x) {
  return max(x, -x);
}
struct S {
  lli x, y, c;
  S(lli x, lli y, lli c) : x(x), y(y), c(c) {}
};
lli h, w, k;
vector<string> a;
lli u, d, l, r, sx, sy;
void bfs(lli x, lli y) {
  queue<S> q;
  q.emplace(x, y, 0);
  while (!q.empty()) {
    S s = q.front();
    q.pop();
    l = min(l, s.x);
    r = max(r, s.x);
    u = min(u, s.y);
    d = max(d, s.y);
    lli vx = 1, vy = 0;
    if (s.c < k) {
      for (lli i = 0; i < 4; ++i) {
        lli tx = x + vx, ty = y + vy;
        if (0 <= tx && tx < w && 0 <= ty && ty < h && a[ty][tx] == '.') {
          a[ty][tx] = '#';
          q.emplace(tx, ty, s.c + 1);
        }
        lli t = vx;
        vx = -vy;
        vy = t;
      }
    }
  }
}
int main() {
  cin >> h >> w >> k;
  read_n(a, h);
  d = r = 0;
  l = w - 1;
  u = h - 1;
  for (lli y = 0; y < h; ++y) {
    for (lli x = 0; x < w; ++x) {
      if (a[y][x] == 'S') {
        sx = x;
        sy = y;
        goto out;
      }
    }
  }
out:
  bfs(sx, sy);
  lli m = min({u, h - 1 - d, l, w - 1 - r});
  cout << 1 + (m + k - 1) / k << endl;
  return 0;
}
