#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int main() {
  const int MAX = 82;
  int H, W, K;
  cin >> H >> W >> K;
  char map[MAX][MAX] = {};
  bool done[MAX][MAX] = {};
  int si, sj;
  for (int i = 0; i < H; i++) {
    scanf("%s", map[i]);
    for (int j = 0; j <= strlen(map[i]); j++) {
      if (map[i][j] == 'S') {
        si = i;
        sj = j;
        map[i][j] = '.';
      }
    }
  }
  int minmagic = 0x3f3f3f3f;
  queue<int> x, y;
  x.push(si);
  y.push(sj);
  done[si][sj] = 1;
  while (x.size() != 0) {
    done[x.front()][y.front()] = 1;
    if (map[x.front()][y.front()] == '.') {
      for (int i = 0; i < 4; i++) {
        if (map[x.front() + dx[i]][y.front() + dy[i]] == '.' &&
            done[x.front() + dx[i]][y.front() + dy[i]] == 0) {
          x.push(x.front() + dx[i]);
          y.push(y.front() + dy[i]);
        }
      }
    }
    x.pop();
    y.pop();
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (done[i][j] == 1) {
        int dis = abs(i - si) + abs(j - sj);
        int mini = 0x3f3f3f3f;
        int mina = min(abs(0 - i), abs(H - i));
        int minb = min(abs(0 - j), abs(W - j));
        mini = min(mina, minb);
        if (dis >= K) {
          minmagic = min(minmagic, (mini + 1) / K);
        } else {
          minmagic = min(minmagic, (mini + 1) / K + 1);
        }
      }
    }
  }
  cout << minmagic << endl;
  return 0;
}
