#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstdlib>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }

int main() {
  std::cin.tie(0); std::ios::sync_with_stdio(false);
  int H, W, K; std::cin >> H >> W >> K;
  auto dist = [&](int h, int w)->int {
    int d = std::min(std::min(h, w), std::min(H-1-h, W-1-w));
    return (d+K-1)/K;
  };
  std::vector<std::string> rooms(H);
  int sH, sW;
  for(int h = 0; h < H; ++h) {
    std::cin >> rooms[h];
    for(int w = 0; w < W; ++w) if(rooms[h][w] == 'S') {
      sH = h;
      sW = w;
    }
  }
  int dh[4] = {0, -1, 0, 1};
  int dw[4] = {-1, 0, 1, 0};
  auto dfs = [&](auto&& f, int h, int w, int hp) {
    int res = dist(h, w);
    if(res == 0) return 0;
    if(hp == 0) return res;
    rooms[h][w] = '#';
    for(int i = 0; i < 4; ++i) {
      int nh = h+dh[i], nw = w+dw[i];
      if(rooms[nh][nw]=='.') res = std::min(res, f(f, nh, nw, hp-1));
    }
    return res;
  };
  fin(dfs(dfs, sH, sW, K)+1);
  return 0;
}
