#include <bits/stdc++.h>
using namespace std;
int H, W, K, S_h, S_w;
string A[800];
int is_first[800][800] = {};
int dh[] = {1, 0, -1, 0}, dw[] = {0, 1, 0, -1};
void go(int h, int w, int k) {
  queue<pair<pair<int, int>, int> > Q;
  Q.push(make_pair(make_pair(h, w), k));
  while (!Q.empty()) {
    pair<pair<int, int>, int> now = Q.front();
    Q.pop();
    int h = now.first.first;
    int w = now.first.second;
    int k = now.second;
    is_first[h][w] = true;
    if (k > 1) {
      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_] < k - 1)) {
          Q.push(make_pair(make_pair(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;
      }
    }
  }
  go(S_h, S_w, K + 1);
  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;
}
