#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
const int N = 810;
int h, w, k, ans, sx, sy;
int used[N][N], d[N][N], q[N * N * 2];
char Map[N][N];
int main() {
  scanf("%d%d%d", &h, &w, &k);
  memset(Map, '#', sizeof(Map));
  for (int i = 1; i <= h; ++i) {
    char s[N];
    scanf("%s", s + 1);
    for (int j = 1; j <= strlen(s + 1); ++j) {
      if (s[j] == 'S') {
        used[i][j] = 1;
        sx = i;
        sy = j;
      }
      Map[i][j] = s[j];
    }
  }
  ans = 1 << 29;
  int l = 1, r = 0;
  q[++r] = sx;
  q[++r] = sy;
  while (l <= r) {
    int x = q[l++], y = q[l++];
    if (d[x][y] > k) continue;
    ans = min(ans, min(min(x - 1, h - x), min(y - 1, w - y)));
    for (int i = 0; i < 4; ++i) {
      int xx = x + dx[i], yy = y + dy[i];
      if (!used[xx][yy] && Map[xx][yy] == '.') {
        used[xx][yy] = 1;
        d[xx][yy] = d[x][y] + 1;
      }
    }
  }
  ans = (ans + k - 1) / k + 1;
  printf("%d\n", ans);
  return 0;
}
