#include <bits/stdc++.h>
using namespace std;
int ceil(int x, int y) {
  if (x % y == 0)
    return x / y;
  else
    return x / y + 1;
}
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> bar(H);
  for (int i = 0; i < H; ++i) {
    cin >> bar[i];
  }
  int sx, sy;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (bar[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  queue<int> qx;
  queue<int> qy;
  auto vis = vector<vector<int>>(H, vector<int>(W, -1));
  int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  vis[sx][sy] = 0;
  qx.push(sx);
  qy.push(sy);
  while (!qx.empty()) {
    int x = qx.front();
    int y = qy.front();
    qx.pop();
    qy.pop();
    int step = vis[x][y] + 1;
    if (step > K) continue;
    for (int i = 0; i < 4; ++i) {
      int nx = x + dir[i][0];
      int ny = y + dir[i][1];
      if (nx >= 0 && nx < H && ny >= 0 && ny < W && vis[nx][ny] == -1) {
        vis[nx][ny] = step;
        qx.push(nx);
        qy.push(ny);
      }
    }
  }
  int ans = 1000000;
  for (int x = 0; x < H; ++x) {
    for (int y = 0; y < W; ++y) {
      if (vis[x][y] == -1) continue;
      ans = min(ans, ceil(x, K));
      ans = min(ans, ceil((H - 1 - x), K));
      ans = min(ans, ceil(y, K));
      ans = min(ans, ceil((W - 1 - y), K));
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
