#include <bits/stdc++.h>
using namespace std;
int H, W, K, S_h, S_w;
string A[800];
bool is_first[800][800] = {};
int dh[] = {1, 0, -1, 0}, dw[] = {0, 1, 0, -1};
void go(int h, int w, int k) {
  is_first[h][w] = true;
  if (k == 0) {
    return;
  }
  for (int i = 0; i < 4; i++) {
    int h_ = h + dh[i];
    int w_ = w + dw[i];
    if (0 <= h_ && h_ < H && 0 <= w_ && w_ < W && A[h_][w_] == '.' &&
        !is_first[h_][w_]) {
      is_first[h_][w_] = true;
      go(h_, w_, k - 1);
    }
  }
}
int main() {
  cin >> H >> W >> K;
  for (int h = 0; h < H; h++) {
    cin >> A[h];
    for (int w = 0; w < W; w++) {
      if (A[h][w] == 'S') {
        S_h = h;
        S_w = w;
      }
    }
  }
  is_first[S_h][S_w] = true;
  go(S_h, S_w, K);
  int ans = 1000;
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      if (is_first[h][w]) {
        int dist = min(h, min(H - 1 - h, min(w, W - 1 - w)));
        ans = min(ans, 1 + (dist + K - 1) / K);
      }
    }
  }
  cout << ans << endl;
}
