#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char map[801][802] = {0};
char visited[801][802] = {0};
int nd = INT_MAX;
void searchNearest(int cx, int cy, int ck) {
  if (cy < 1 || h < cy || cx < 1 || w < cx) return;
  if (map[cy][cx] == '#') return;
  if (visited[cy][cx] != 0) return;
  visited[cy][cx] = 1;
  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 > 0) {
    --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 = 0, sy = 0;
  for (int i = 1; i <= h; ++i) {
    cin >> &map[i][1];
    if (sx == 0) {
      for (int j = 1; 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 != 0) ++count;
  cout << count << endl;
}
