#include <bits/stdc++.h>
using namespace std;
char a[1000][1000];
int dp[1000][1000];
int H, W, K;
queue<pair<int, int> > q;
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 dfs(int h, int w, int k) {
  dp[h][w] = k;
  int mi = min_to_go(h, w);
  if (k == K) return mi;
  for (int i = 0; i < 4; i++) {
    if (h + vh[i] == -1 || h + vh[i] == H) continue;
    if (w + vw[i] == -1 || w + vw[i] == W) continue;
    if (a[h + vh[i]][w + vw[i]] == '#') continue;
    if (dp[h + vh[i]][w + vw[i]] > k + 1)
      mi = min(mi, dfs(h + vh[i], w + vw[i], k + 1));
  }
  return 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;
    }
  }
  int mi = dfs(sh, sw, 0);
  cout << 1 + d(mi) << endl;
  return 0;
}
