#include <bits/stdc++.h>
using namespace std;
int dist[810][810];
int pass[810][810];
int used[810][810];
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dist[i][j] = 1000000007;
    }
  }
  queue<int> que;
  for (int i = 0; i < h; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < w; j++) {
      if (str[j] == '#') {
        pass[i][j] = 1;
      } else if (str[j] == 'S') {
        dist[i][j] = 0;
        que.push(i * w + j);
        used[i][j] = 1;
      }
    }
  }
  int qsc[5] = {+1, 0, -1, 0, +1};
  while (!que.empty()) {
    int tmp = que.front();
    que.pop();
    int ci = tmp / w;
    int cj = tmp % w;
    for (int i = 0; i < 4; i++) {
      int di = ci + qsc[i];
      int dj = cj + qsc[i + 1];
      if (0 <= di && di < h && 0 <= dj && dj < w && used[di][dj] == 0 &&
          pass[di][dj] == 0) {
        used[di][dj] = 1;
        dist[di][dj] = min(dist[di][dj], dist[ci][cj] + 1);
        que.push(di * w + dj);
      }
    }
  }
  int nst = 1000000007;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (dist[i][j] <= k) {
        int cdis = min(i, min(h - i, min(j, w - j)));
        if (nst > cdis) nst = cdis;
      }
    }
  }
  int ans = 1;
  ans += nst / k;
  if (nst % k != 0) ans++;
  cout << ans << endl;
  return 0;
}
