#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (auto &e : a) cin >> e;
  long si, sj;
  for (long i = 0; i < h; i++)
    for (long j = 0; j < w; j++)
      if (a[i][j] == 'S') si = i, sj = j;
  long dxy[] = {-1, 0, 1, 0, -1};
  set<pair<long, long>> s;
  {
    queue<tuple<long, long, long>> q;
    q.push(make_tuple(0, si, sj));
    while (!q.empty()) {
      long d, y, x;
      tie(d, y, x) = q.front();
      q.pop();
      if (d == k) continue;
      for (long di = 0; di < 4; di++) {
        long dx = dxy[di], dy = dxy[di + 1];
        long nx = x + dx, ny = y + dy;
        if (!(0 <= nx && nx < w && 0 <= ny && ny < h)) continue;
        if (s.count({ny, nx})) continue;
        if (a[ny][nx] == '#') continue;
        s.insert({ny, nx});
        q.push(make_tuple(d + 1, ny, nx));
      }
    }
  }
  auto f = [&](pair<long, long> t) {
    long y, x;
    tie(y, x) = t;
    return min(min(x, y), min(w - 1 - x, h - 1 - y));
  };
  long r = 1e9;
  for (auto &t : s) {
    long u = (f(t) + k - 1) / k;
    r = min(r, u);
  }
  cout << 1 + r << endl;
}
