#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (auto& i : A) cin >> i;
  const int dy[4] = {0, 0, 1, -1};
  const int dx[4] = {1, -1, 0, 0};
  int sy, sx;
  for (auto i = 0; i < H; ++i)
    for (auto j = 0; j < W; ++j)
      if (A[i][j] == 'S') sy = i, sx = j;
  auto res = H * W;
  using P = pair<int, int>;
  queue<P> q;
  q.emplace(sy, sx);
  while (!q.empty()) {
    auto y = q.front().first;
    auto x = q.front().second;
    q.pop();
    auto d = min({y, H - 1 - y, x, W - 1 - x});
    res = min(res, 1 + (d + K - 1) / K);
    for (auto i = 0; i < 4; ++i) {
      auto ny = y + dy[i];
      auto nx = x + dx[i];
      if (ny < 0 || H <= ny) continue;
      if (nx < 0 || W <= nx) continue;
      if (A[ny][nx] != '.') continue;
      A[ny][nx] = '#';
      q.emplace(ny, nx);
    }
  }
  cout << res << endl;
}
