#include <bits/stdc++.h>
using namespace std;
bool inside(int x, int y, int xlim, int ylim) {
  return 0 <= x && x < xlim && 0 <= y && y < ylim;
}
struct puni {
  int x, y, d;
  puni(int x, int y, int d) {
    this->x = x;
    this->y = y;
    this->d = d;
  }
};
int main() {
  constexpr int dd[] = {0, 1, 0, -1, 0};
  int h, w, k;
  cin >> h >> w >> k;
  string s[h];
  for (int i = 0; i < h; i++) cin >> s[i];
  queue<puni> q;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (s[i][j] == 'S') {
        s[i][j] = '.';
        q.push(puni(i, j, 0));
      }
  int ans = 1e9;
  while (!q.empty()) {
    puni p = q.front();
    q.pop();
    if (k < p.d) continue;
    int d = min({p.x, p.y, h - p.x, w - p.y});
    ans = min(ans, (d + k - 1) / k);
    for (int i = 0; i < 4; i++) {
      int nx = p.x + dd[i], ny = p.y + dd[i + 1];
      if (inside(nx, ny, h, w) && s[nx][ny] == '.') {
        q.push(puni(nx, ny, p.d + 1));
      }
    }
  }
  cout << ans + 1 << endl;
}
