#include <bits/stdc++.h>
typedef struct point2d {
  int x, y;
} point;
int calc(point p, int h, int w, int k) {
  int a = (p.x + k - 1) / k;
  int b = (h - 1 - p.x + k - 1) / k;
  int c = (p.y + k - 1) / k;
  int d = (w - 1 - p.y + k - 1) / k;
  return (
      (a) < (((b) < (((c) < (d) ? (c) : (d))) ? (b)
                                              : (((c) < (d) ? (c) : (d)))))
          ? (a)
          : (((b) < (((c) < (d) ? (c) : (d))) ? (b)
                                              : (((c) < (d) ? (c) : (d))))));
}
void run(void) {
  int h, w, k;
  scanf("%d%d%d", &h, &w, &k);
  char *board = (char *)calloc(h * w + 1, sizeof(char));
  int i, j;
  for (i = 0; i < h; i++) scanf("%s", board + i * w);
  point s = {0, 0};
  for (i = 0; i < h * w; i++) {
    if (board[i] == 'S') {
      s = (point){i / w, i % w};
      break;
    }
  }
  int min = h * w;
  point *now = (point *)calloc(h * w, sizeof(point));
  point *next = (point *)calloc(h * w, sizeof(point));
  int top = 0;
  now[top++] = s;
  board[((s.x) * w + (s.y))] = '#';
  for (i = 1; i <= k; i++) {
    int ntop = 0;
    while (top > 0) {
      point t = now[--top];
      min = ((min) < (calc(t, h, w, k) + 1) ? (min) : (calc(t, h, w, k) + 1));
      int d[] = {1, 0, -1, 0};
      for (j = 0; j < 4; j++) {
        int x = t.x + d[j];
        int y = t.y + d[j ^ 1];
        if (0 <= x && x < h && 0 <= y && y < w && board[((x)*w + (y))] != '#') {
          board[((x)*w + (y))] = '#';
          next[ntop++] = (point){x, y};
        }
      }
    }
    point *swap = now;
    now = next;
    next = swap;
    top = ntop;
  }
  printf("%d\n", min);
}
int main(void) {
  run();
  return 0;
}
