#include <bits/stdc++.h>
auto& in = std::cin;
auto& out = std::cout;
int32_t H, W, K;
char MAP[810][810];
int32_t D[810][810];
template <typename T>
void fill_all(T& arr, const T& v) {
  arr = v;
}
template <typename T, typename ARR>
void fill_all(ARR& arr, const T& v) {
  for (auto& i : arr) {
    fill_all(i, v);
  }
}
int32_t D_min = 800 * 800 * 800;
void func(int x, int y, int k) {
  if (x < 0 || y < 0 || x >= W || y >= H) {
    return;
  }
  if (D[y][x] != -1) {
    return;
  }
  if (k < 0) {
    return;
  }
  if (MAP[y][x] == '#') {
    return;
  }
  D[y][x] = std::min(std::min(y, H - y - 1), std::min(x, W - x - 1));
  D_min = std::min(D_min, D[y][x]);
  func(x - 1, y, k - 1);
  func(x + 1, y, k - 1);
  func(x, y - 1, k - 1);
  func(x, y + 1, k - 1);
}
int main() {
  fill_all(D, -1);
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in >> H >> W >> K;
  for (int32_t i = 0; i < H; ++i) {
    in >> MAP[i];
  }
  for (int32_t h = 0; h < H; ++h)
    for (int32_t w = 0; w < W; ++w) {
      if (MAP[h][w] == 'S') {
        func(w, h, K);
        break;
      }
    }
  std::cout << 1 + (D_min / K) + (((D_min % K) > 0) ? 1 : 0) << std::endl;
  return 0;
}
