#include <bits/stdc++.h>
using namespace std;
char heya[800][802];
char (*heya_)[802] = heya;
int h, w, k;
int main() {
  cin >> h >> w >> k;
  {
    char tmp[5];
    cin.getline(tmp, 5);
  }
  for (int i = 0; i < h; i++) {
    cin.getline(heya[i], 802);
  }
  int sx, sy;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (heya[i][j] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }
  heya[sy][sx] = '.';
  queue<pair<pair<int, int>, int>> q;
  q.push(make_pair(make_pair(sx, sy), 0));
  while (!q.empty()) {
    auto a = q.front();
    q.pop();
    int x = a.first.first, y = a.first.second, kk = a.second;
    if (heya[y][x] != '.') continue;
    heya[y][x] = 'S';
    if (kk < k) {
      if (sx - 1 >= 0) q.push(make_pair(make_pair(sx - 1, sy), kk + 1));
      if (sy - 1 >= 0) q.push(make_pair(make_pair(sx, sy - 1), kk + 1));
      if (sx + 1 < w) q.push(make_pair(make_pair(sx + 1, sy), kk + 1));
      if (sy + 1 < h) q.push(make_pair(make_pair(sx, sy + 1), kk + 1));
    }
  }
  int best = 99999999;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (heya[i][j] == 'S') {
        best = min(best, i);
        best = min(best, j);
        best = min(best, h - 1 - i);
        best = min(best, w - 1 - j);
      }
    }
  }
  if (best == 0) {
    cout << 1 << endl;
    return 0;
  }
  cout << (best - 1) / k + 2 << endl;
  return 0;
}
