#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<char>> field(h, vector<char>(w));
  int x, y;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> field[i][j];
      if (field[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  vector<vector<bool>> d(h, vector<bool>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = false;
    }
  }
  function<int(int, int, int)> bfs = [&](int x, int y, int count) {
    queue<P> que;
    que.push(P(x, y));
    d[x][y] = true;
    int tmp = min(min(x, h - x - 1), min(y, w - y - 1));
    while (count--) {
      queue<P> nque;
      while (que.size()) {
        P p = que.front();
        que.pop();
        int mi =
            min(min(p.first, h - p.first - 1), min(p.second, w - p.second - 1));
        tmp = min(tmp, mi);
        for (int i = 0; i < 4; i++) {
          int nx = p.first + dx[i];
          int ny = p.second + dy[i];
          if (0 <= nx && nx < h && 0 <= ny && ny < w && field[nx][ny] == '.' &&
              d[nx][ny] == false) {
            d[nx][ny] = true;
            nque.push(P(nx, ny));
          }
        }
      }
      que = nque;
    }
    return tmp;
  };
  int mi = bfs(x, y, k);
  if (mi == 0)
    cout << 1 << endl;
  else {
    cout << (mi - 1) / k + 2 << endl;
  }
}
