#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char map[801][801];
int nd;
void searchNearest(int cx, int cy, int ck) {
  if (cy < 0 || h <= cy || cx < 0 || w <= cx) return;
  if (map[cy][cx] == '#') return;
  int dy = (h / 2 > cy) ? cy : h - cy;
  int dx = (w / 2 > cx) ? cx : w - cx;
  int md = (dy < dx) ? dy : dx;
  if (md < nd) nd = md;
  if (ck > 1) {
    --ck;
    searchNearest(cx - 1, cy, ck);
    searchNearest(cx + 1, cy, ck);
    searchNearest(cx, cy - 1, ck);
    searchNearest(cx, cy + 1, ck);
  }
}
int main() {
  cin >> h >> w >> k;
  int sx = -1, sy;
  for (int i = 0; i < h; ++i) {
    cin >> map[i];
    if (sx < 0) {
      for (int j = 0; j < w; ++j) {
        if (map[i][j] == 'S') {
          sx = j;
          sy = i;
          break;
        }
      }
    }
  }
  searchNearest(sx, sy, k);
  int count = (nd / k + 1);
  if (nd % k) ++count;
  cout << count << endl;
}
