#include <bits/stdc++.h>
int MIN(int a, int b) { return a < b ? a : b; }
int Y[] = {0, 1, 0, -1};
int X[] = {1, 0, -1, 0};
int main() {
  int h, w, n, ny, nx, min = 1e9, i, j, x[800010], y[800010], t = 0, r, a;
  char d[1010][1010] = {};
  int c[1010][1010] = {};
  scanf("%d %d %d", &h, &w, &n);
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      scanf(" %c", &d[i][j]);
      if (d[i][j] == 'S') d[y[0] = i][x[0] = j] = '#';
    }
  }
  t = 0;
  min = MIN(MIN(y[0] - 1, h - y[0]), MIN(x[0] - 1, w - x[0]));
  for (r = 1; r - t; t++) {
    for (i = 0; i < 4; i++) {
      ny = y[t] + Y[i];
      nx = x[t] + X[i];
      if (d[ny][nx] - '.') continue;
      c[ny][nx] = c[y[t]][x[t]] + 1;
      d[y[r] = ny][x[r] = nx] = '#';
      if (c[ny][nx] <= n) {
        a = MIN(MIN(ny - 1, h - ny), MIN(nx - 1, w - nx));
        a = a / n;
        min = MIN(min, a);
        r++;
      }
    }
  }
  printf("%d\n", min + 1);
  return 0;
}
