#include <bits/stdc++.h>
using namespace std;
using ll = long long;
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)> dfs = [&](int x, int y, int count) {
    d[x][y] = true;
    int tmp = min(min(x, h - x - 1), min(y, w - y - 1));
    if (count == 0) return tmp;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && field[nx][ny] == '.' &&
          d[nx][ny] == false) {
        tmp = min(tmp, dfs(nx, ny, count - 1));
      }
    }
    return tmp;
  };
  int mi = dfs(x, y, k);
  if (mi == 0)
    cout << 1 << endl;
  else {
    cout << (mi - 1) / k + 2 << endl;
  }
}
