#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int dh[] = {1, -1, 0, 0};
const int dw[] = {0, 0, 1, -1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> a(H);
  for (int i = 0; i < H; i++) {
    cin >> a[i];
  }
  int ch = -1, cw;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (a[i][j] == 'S') {
        ch = i;
        cw = j;
        a[i][j] = '.';
        break;
      }
    }
    if (ch != -1) break;
  }
  vector<vector<int> > memo(H, vector<int>(W, INF));
  queue<pair<int, int> > q;
  memo[ch][cw] = 0;
  q.push(pair<int, int>(ch, cw));
  while (!q.empty()) {
    int h, w;
    tie(h, w) = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nh = h + dh[i], nw = w + dw[i];
      if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
      if (a[nh][nw] == '#') continue;
      if (memo[nh][nw] != INF) continue;
      memo[nh][nw] = memo[h][w] + 1;
      q.push(pair<int, int>(nh, nw));
    }
  }
  int ans = INF;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (memo[i][j] == INF) continue;
      int ma = memo[i][j] / K + 1;
      int tmp = INF;
      tmp = min(tmp, i);
      tmp = min(tmp, j);
      tmp = min(tmp, H - 1 - i);
      tmp = min(tmp, W - 1 - j);
      ma += (tmp + K - 1) / K;
      ans = min(ans, ma);
    }
  }
  cout << ans << endl;
  return 0;
}
