#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);
}
lli h, w, k;
vector<string> a;
lli u, d, l, r, sx, sy;
void dfs(lli x, lli y, lli p) {
  if (x < 0 || w <= x || y < 0 || h <= y || a[y][x] == '#' || p < 0) return;
  a[y][x] = '#';
  if (y < u) u = y;
  if (y > d) d = y;
  if (x < l) l = x;
  if (x > r) r = x;
  lli vx = 1, vy = 0;
  for (lli i = 0; i < 4; ++i) {
    dfs(x + vx, y + vy, p);
    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:
  dfs(sx, sy, k);
  lli m = min({u, h - 1 - d, l, w - 1 - r});
  cout << 1 + (m + k - 1) / k << endl;
  return 0;
}
