#include <bits/stdc++.h>
using namespace std;
int n, m, k, startx, starty, head, tail;
char c[803][803];
int dist[803][803], q[101000][2];
void ins(int x, int y, int d) {
  if (dist[x][y] <= d || c[x][y] == '#') return;
  dist[x][y] = d;
  tail++;
  q[tail][0] = x, q[tail][1] = y;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%s", c[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (c[i][j] == 'S') startx = i, starty = j, c[i][j] = '.';
      dist[i][j] = 1e9;
    }
  }
  ins(startx, starty, 0);
  while (head < tail) {
    head++;
    int x = q[head][0], y = q[head][1];
    ins(x + 1, y, dist[x][y] + 1);
    ins(x - 1, y, dist[x][y] + 1);
    ins(x, y + 1, dist[x][y] + 1);
    ins(x, y - 1, dist[x][y] + 1);
  }
  int res = 1e9;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (dist[i][j] <= k) {
        res = min(res,
                  1 + (min(min(i - 1, n - i), min(j - 1, m - j)) + k - 1) / k);
      }
    }
  printf("%d", res);
  return 0;
}
