#include <bits/stdc++.h>
using namespace std;
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};
int h, w;
vector<vector<char>> a(800, vector<char>(800));
vector<vector<bool>> done(800, vector<bool>(800));
void dfs(int y, int x, int depth) {
  if (done.at(y).at(x) || depth == 0) return;
  done.at(y).at(x) = true;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if ((0 <= ny && ny < h) && (0 <= nx && nx < w) && (a[ny][nx] == '.'))
      dfs(ny, nx, depth - 1);
  }
}
int main() {
  int k;
  cin >> h >> w >> k;
  int sy = 0, sx = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a.at(i).at(j);
      if (a.at(i).at(j) == 'S') {
        sy = i;
        sx = j;
        a.at(i).at(j) = '.';
      }
    }
  }
  dfs(sy, sx, k + 1);
  int mini = 800 * 800;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!done.at(i).at(j)) continue;
      if ((i == 0) || (i == h - 1) || (j == 0) || (j == w - 1))
        mini = 1;
      else
        mini = min(mini, (min({i, h - i, j, w - j}) + k - 1) / k + 1);
    }
  }
  cout << mini << endl;
}
