#include <bits/stdc++.h>
using namespace std;
long long int res;
long long int h, w, k;
vector<string> a;
bool visited[1000][1000];
void dfs(int r, int c, long long int nokori) {
  if (visited[r][c]) {
    return;
  }
  visited[r][c] = 1;
  long long int curres;
  if ((r == 0) || (c == 0) || (r == h - 1) || (c == w - 1)) {
    curres = 1;
  } else {
    curres = min((r + k - 1) / k,
                 min((c + k - 1) / k,
                     min((h - 1 - r + k - 1) / k, (w - 1 - c + k - 1) / k))) +
             1;
  }
  res = min(res, curres);
  if (nokori > 0) {
    if (r > 0 && a[r - 1][c] != '#') dfs(r - 1, c, nokori - 1);
    if (r < h - 1 && a[r + 1][c] != '#') dfs(r + 1, c, nokori - 1);
    if (c > 0 && a[r][c - 1] != '#') dfs(r, c - 1, nokori - 1);
    if (c < w - 1 && a[r][c + 1] != '#') dfs(r, c + 1, nokori - 1);
  }
}
int main(void) {
  cin >> h >> w >> k;
  a = vector<string>(h);
  int sr, sc;
  for (int i = 0; i < h; i++) {
    cin >> a[i];
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        sr = i;
        sc = j;
      }
    }
  }
  res = 1e18;
  dfs(sr, sc, k);
  cout << res << endl;
  return 0;
}
