#include <bits/stdc++.h>
int w, h, k;
int is_edge(int ii, int jj) {
  return ii == 0 || jj == 0 || ii == h - 1 || jj == w - 1;
}
void search(int ii, int jj, int kk, char** a, int** b) {
  if ((a[ii][jj] == '.' || a[ii][jj] == 'S') && kk >= 0) {
    b[ii][jj] = 1;
  } else if (a[ii][jj] == '#') {
    b[ii][jj] = -1;
  }
  if (b[ii][jj] == 1 && is_edge(ii, jj) == 0) {
    if (b[ii][jj - 1] == 0) {
      search(ii, jj - 1, kk - 1, a, b);
    }
    if (b[ii][jj + 1] == 0) {
      search(ii, jj + 1, kk - 1, a, b);
    }
    if (b[ii - 1][jj] == 0) {
      search(ii - 1, jj, kk - 1, a, b);
    }
    if (b[ii + 1][jj] == 0) {
      search(ii + 1, jj, kk - 1, a, b);
    }
  }
}
int min(int ii, int jj) {
  if (ii < jj) {
    return ii;
  } else {
    return jj;
  }
}
int main() {
  char** a = (char**)malloc(800 * sizeof(char*));
  int** b = (int**)malloc(800 * sizeof(int*));
  int i, j;
  for (i = 0; i < 800; i = i + 1) {
    a[i] = (char*)malloc(800 * sizeof(char));
  }
  for (i = 0; i < 800; i = i + 1) {
    b[i] = (int*)malloc(800 * sizeof(int));
  }
  int x, y, xy;
  int si, sj;
  scanf("%d %d %d", &h, &w, &k);
  for (i = 0; i < h; i = i + 1) {
    scanf("%s", a[i]);
  }
  for (i = 0; i < h; i = i + 1) {
    for (j = 0; j < w; j = j + 1) {
      if (a[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  search(si, sj, k, a, b);
  int ans = w;
  for (i = 0; i < h; i = i + 1) {
    for (j = 0; j < w; j = j + 1) {
      if (b[i][j] == 1) {
        x = ceil(min(i, h - i - 1) * 1.0 / k) + 1;
        y = ceil(min(j, w - j - 1) * 1.0 / k) + 1;
        xy = min(x, y);
        ans = min(xy, ans);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
