#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int d[803][803];
char a[803][803];
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') sx = i, sy = j;
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) d[i][j] = -1;
  queue<pair<int, int> > que;
  d[sx][sy] = 0;
  que.push(pair<int, int>(sx, sy));
  while (que.size()) {
    pair<int, int> now = que.front();
    int x = now.first, y = now.second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      if (x + dx[i] >= 0 && x + dx[i] < h && y + dy[i] >= 0 && y + dy[y] < w &&
          d[x + dx[i]][y + dy[i]] == -1 && a[x + dx[i]][y + dy[i]] == '.' &&
          d[x][y] < k) {
        d[x + dx[i]][y + dy[i]] = d[x][y] + 1;
        que.push(pair<int, int>(x + dx[i], y + dy[i]));
      }
    }
  }
  int les = 1000000;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (d[i][j] != -1)
        les = min(les, min(min(i, j), min(h - 1 - i, w - 1 - j)));
  cout << (les + k - 1) / k + 1 << endl;
}
