#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;
struct State {
  int y, x, c;
  State(int y_, int x_, int c_) : y(y_), x(x_), c(c_) {}
};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> field(H);
  for (int i = 0; i < H; i++) {
    cin >> field[i];
  }
  int si = -1, sj = -1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  queue<State> que;
  que.emplace(si, sj, 0);
  vector<vector<bool>> visited(H, vector<bool>(W, false));
  visited[si][sj] = true;
  const int dd[] = {0, 1, 0, -1, 0};
  int ans = H * W + 1;
  while (que.size()) {
    auto s = que.front();
    que.pop();
    if (s.y == 0 || s.y == H - 1 || s.x == 0 || s.x == W - 1) {
      ans = 1;
      break;
    }
    if (s.c == K) break;
    int shortest = min(min(s.y, H - 1 - s.y), min(s.x, W - 1 - s.x));
    ans = min(ans, 1 + (shortest + K - 1) / K);
    for (int i = 0; i < 4; i++) {
      int ny = s.y + dd[i], nx = s.x + dd[i + 1];
      if (ny < 0 || H <= ny || nx < 0 || W <= nx || field[ny][nx] == '#')
        continue;
      if (!visited[ny][nx]) {
        visited[ny][nx] = true;
        que.emplace(ny, nx, s.c + 1);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
