#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<bool>> board(H, vector<bool>(W));
  pair<int, int> S;
  for (int i = 0; i < H; i++) {
    string A;
    cin >> A;
    for (int j = 0; j < W; j++) {
      if (A[j] == 'S') {
        S = make_pair(i, j);
      } else if (A[j] == '#') {
        board[i][j] = true;
      }
    }
  }
  auto calc = [H, W](const pair<int, int>& p) {
    return min({p.first, (H - 1) - p.first, p.second, (W - 1) - p.second});
  };
  vector<vector<bool>> done(H, vector<bool>(W));
  priority_queue<tuple<int, int, pair<int, int>>> q;
  int score = -calc(S);
  q.emplace(score, 0, S);
  while (!q.empty()) {
    pair<int, int> pos;
    int K1;
    int tmp;
    tie(tmp, K1, pos) = q.top();
    q.pop();
    if (score < tmp) {
      S = pos;
      score = tmp;
    }
    done[pos.first][pos.second] = true;
    if (pos.first == 0 || pos.first == H - 1 || pos.second == 0 ||
        pos.second == W - 1) {
      break;
    }
    if (K1 == K) break;
    static const int dx[4] = {0, 1, 0, -1};
    static const int dy[4] = {1, 0, -1, 0};
    for (int d = 0; d < 4; d++) {
      auto p2 = pos;
      p2.first += dx[d];
      p2.second += dy[d];
      if (!board[p2.first][p2.second] && !done[p2.first][p2.second]) {
        q.emplace(-calc(p2), K1 + 1, p2);
      }
    }
  }
  int ans = 1;
  ans += (calc(S) + K - 1) / K;
  cout << ans << endl;
  return 0;
}
