#include <bits/stdc++.h>
using namespace std;
char a[1000][1000];
int dp[1000][1000];
int H, W, K;
queue<int> qh, qw, qk;
int vh[4] = {1, -1, 0, 0};
int vw[4] = {0, 0, 1, -1};
int sh, sw;
int min_to_go(int h, int w) {
  int hh = min(H - h, h);
  int ww = min(W - w, w);
  return min(hh, ww);
}
int bfs(int m) {
  if (qh.empty()) return m;
  int h = qh.front();
  qh.pop();
  int w = qw.front();
  qw.pop();
  int k = qk.front();
  qk.pop();
  int mi = min(m, min_to_go(h, w));
  if (k >= K) return bfs(mi);
  for (int i = 0; i < 4; i++) {
    int hh = h + vh[i], ww = w + vw[i];
    if (hh < 0 || hh >= H) continue;
    if (ww < 0 || ww >= W) continue;
    if (a[hh][ww] == '.' && dp[hh][ww] == 1e9) {
      dp[hh][ww] = k + 1;
      qh.push(hh);
      qw.push(ww);
      qk.push(k + 1);
    }
  }
  return bfs(mi);
}
int d(int x) {
  if (x % K == 0)
    return x / K;
  else
    return x / K + 1;
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sh = i;
        sw = j;
      }
      dp[i][j] = 1e9;
    }
  }
  qh.push(sh);
  qw.push(sw);
  qk.push(0);
  dp[sh][sw] = 0;
  int mi = bfs(1e9);
  cout << 1 + d(mi) << endl;
  return 0;
}
