#include <bits/stdc++.h>
using namespace std;
int calc(int i, int j, int H, int W, int K) {
  int ret = 10000;
  ret = min(ret, i);
  ret = min(ret, j);
  ret = min(ret, H - 1 - i);
  ret = min(ret, W - 1 - j);
  return (ret + K - 1) / K;
}
int main(void) {
  ios::sync_with_stdio(false);
  string S[801];
  int H, W, K;
  queue<pair<int, int> > q;
  int ret = 1000;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == 'S') {
        q.push(pair<int, int>(i * 1000 + j, K));
        goto L1;
      }
    }
  }
L1:
  while (!q.empty()) {
    int px = q.front().first % 1000;
    int py = q.front().first / 1000;
    int cnt = q.front().second;
    q.pop();
    ret = min(ret, calc(py, px, H, W, K));
    if (ret == 0) break;
    S[py][px] = 'S';
    if (cnt > 0) {
      if (px > 0 && S[py][px - 1] == '.')
        q.push(pair<int, int>(py * 1000 + px - 1, cnt - 1));
      if (px < W - 1 && S[py][px + 1] == '.')
        q.push(pair<int, int>(py * 1000 + px + 1, cnt - 1));
      if (py > 0 && S[py - 1][px] == '.')
        q.push(pair<int, int>((py - 1) * 1000 + px, cnt - 1));
      if (py < H - 1 && S[py + 1][px] == '.')
        q.push(pair<int, int>((py + 1) * 1000 + px, cnt - 1));
    }
  }
  cout << ret + 1 << endl;
}
