#include <bits/stdc++.h>
int H, W, K;
char map[(800) + 1][(800) + 1];
int q[(800) * (800)][2];
int is_edge(int x, int y) {
  return x == 0 || y == 0 || (H - 1) == x || (W - 1) == y;
}
int min(int x, int y) { return x < y ? x : y; }
int score(int p[2]) {
  int x = p[0];
  int y = p[1];
  return min(min(x, H - x), min(y, W - y));
}
void core() {
  int sx = 0, sy = 0;
  scanf("%d%d%d", &H, &W, &K);
  for (int i = (0); i < (H); i++) {
    scanf("%s", map[i]);
    for (int j = (0); j < (W); j++) {
      if (map[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  int l = 0, r = 1;
  q[0][0] = sx;
  q[0][1] = sy;
  for (int k = (0); k < (K); k++) {
    if (l == r) {
      break;
    }
    int d = 0;
    for (int i = (l); i < (r); i++) {
      int x = q[i][0];
      int y = q[i][1];
      map[x][y] = 'x';
      if (!is_edge(x, y)) {
        if (map[x - 1][y] == '.') {
          q[r + d][0] = x - 1;
          q[r + d][1] = y;
          d++;
        }
        if (map[x + 1][y] == '.') {
          q[r + d][0] = x + 1;
          q[r + d][1] = y;
          d++;
        }
        if (map[x][y - 1] == '.') {
          q[r + d][0] = x;
          q[r + d][1] = y - 1;
          d++;
        }
        if (map[x][y + 1] == '.') {
          q[r + d][0] = x;
          q[r + d][1] = y + 1;
          d++;
        }
      }
    }
    l = r;
    r += d;
  }
  int min_score = score(q[0]);
  for (int i = (1); i < (r); i++) {
    min_score = min(min_score, score(q[i]));
  }
  printf("%d\n", (int)ceil(((double)min_score) / K) + 1);
}
int main() {
  core();
  return 0;
}
