#include <bits/stdc++.h>
using namespace std;
const int N = 880;
int i, j, k, n, m, K, x, y, ans;
int c[N][N];
int bx[4] = {1, -1, 0, 0};
int by[4] = {0, 0, 1, -1};
int dx[N * N], dy[N * N];
char s[N][N];
int main() {
  scanf("%d%d%d", &n, &m, &K);
  for (i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (j = 1; j <= m; j++)
      if (s[i][j] == 'S') x = i, y = j;
  }
  i = 0;
  j = 1;
  dx[1] = x;
  dy[1] = y;
  c[x][y] = 1;
  while (i < j) {
    i++;
    int dis = c[dx[i]][dy[i]];
    if (dis == m + 1) break;
    for (k = 0; k < 4; k++) {
      int xx = dx[i] + bx[k];
      int yy = dy[i] + by[k];
      if (1 <= xx && xx <= n && 1 <= yy && yy <= m && !c[xx][yy] &&
          s[xx][yy] != '#') {
        c[xx][yy] = dis + 1;
        j++;
        dx[j] = xx;
        dy[j] = yy;
      }
    }
  }
  ans = n * m;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (c[i][j]) {
        int t1 = min(i - 1, n - i);
        int t2 = min(j - 1, m - j);
        ans = min(ans, (min(t1, t2) + K - 1) / K);
      }
  printf("%d\n", ans + 1);
}
