#include <bits/stdc++.h>
using namespace std;
const int big = ((int)1 << 30);
int main(void) {
  int H, W, K, i, j, n, sx, sy, x, y, ka, sai = big;
  queue<tuple<int, int, int>> que;
  vector<string> hey;
  vector<vector<bool>> ot;
  cin >> H >> W >> K;
  hey.resize(H);
  ot.resize(H);
  for (i = 0; i < H; i++) {
    cin >> hey[i];
  }
  for (i = 0; i < H; i++) {
    ot[i].resize(W);
    for (j = 0; j < W; j++) {
      if (hey[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  que.push(make_tuple(sx, sy, K));
  while (!que.empty()) {
    x = get<0>(que.back());
    y = get<1>(que.back());
    ka = get<2>(que.back());
    que.pop();
    if (ka == -1) {
      break;
    }
    if (!ot[x][y]) {
      ot[x][y] = true;
    }
    sai = min(sai, min(min(x, H - x - 1), min(y, W - y - 1)));
    if (sai == 0) {
      break;
    }
    if (hey[x + 1][y] != '#') {
      que.push(make_tuple(x + 1, y, ka - 1));
    }
    if (hey[x - 1][y] != '#') {
      que.push(make_tuple(x - 1, y, ka - 1));
    }
    if (hey[x][y + 1] != '#') {
      que.push(make_tuple(x, y + 1, ka - 1));
    }
    if (hey[x][y - 1] != '#') {
      que.push(make_tuple(x, y - 1, ka - 1));
    }
  }
  cout << (sai + K - 1) / K + 1 << endl;
  return 0;
}
