#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int sx, sy;
  vector<bool> opened(H * W);
  vector<bool> reached(H * W);
  auto Index = [&](int x, int y) -> int { return x + y * W; };
  for (int i = 0; i < H * W; i++) {
    char m;
    cin >> m;
    if (m == 'S') {
      sx = i % W;
      sy = i / W;
    }
    opened[i] = (m != '#') ? true : false;
    reached[i] = false;
  }
  int MC = 1;
  int sr;
  priority_queue<pair<int, int> > moveque;
  auto push_move = [&](int range, int x, int y) {
    if (range < 0) return;
    int index = Index(x, y);
    if (!opened[index] || reached[index]) return;
    moveque.push({range, index});
    reached[index] = true;
    sr = min(min(x, W - x - 1), min(y, H - y - 1));
  };
  push_move(K, sx, sy);
  while (moveque.size()) {
    auto val = moveque.top();
    moveque.pop();
    int x = val.second % W;
    int y = val.second / W;
    if (x > 0) push_move(val.first - 1, x - 1, y);
    if (x < W - 1) push_move(val.first + 1, x + 1, y);
    if (y > 0) push_move(val.first - 1, x, y - 1);
    if (y < H - 1) push_move(val.first - 1, x, y + 1);
  };
  if (sr == 0)
    MC = 1;
  else {
    MC = (sr - 1) / K + 2;
  }
  cout << MC;
  return 0;
}
