#include <bits/stdc++.h>
using namespace std;
int main() {
  constexpr int INF = 1e9;
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> grid(H);
  for (int i = 0; i < H; ++i) {
    cin >> grid[i];
  }
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {1, -1, 0, 0};
  auto in = [&](int x, int y) -> bool {
    return x >= 0 && x < W && y >= 0 && y < H;
  };
  int Sx = 0, Sy = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (grid[i][j] == 'S') {
        Sx = j, Sy = i;
      }
    }
  }
  vector<vector<int>> distS(H, vector<int>(W, INF));
  distS[Sy][Sx] = 0;
  {
    deque<pair<int, int>> que;
    que.emplace_back(Sy, Sx);
    while (!que.empty()) {
      auto p = que.front();
      que.pop_front();
      int x = p.second, y = p.first;
      for (int k = 0; k < 4; ++k) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        if (!in(nx, ny) || grid[ny][nx] == '#' ||
            distS[ny][nx] <= distS[y][x] + 1) {
          continue;
        }
        distS[ny][nx] = distS[y][x] + 1;
        que.emplace_back(ny, nx);
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (distS[i][j] == INF) {
        continue;
      }
      int cost = min({i, j, H - 1 - i, W - 1 - j});
      ans = min(ans, (cost + K - 1) / K + 1);
    }
  }
  cout << ans << endl;
  return 0;
}
