#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int inline in() {
  int x = 0, c;
  for (; (unsigned)((c = getchar()) - '0') >= 10;) {
    if (c == '-') return -in();
    if (!~c) throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((unsigned)((c = getchar()) - '0') < 10);
  return x;
}
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int n, m, k;
char s[805][805];
int dist[805][805];
int si, sj;
void bfs() {
  memset(dist, -1, sizeof(dist));
  queue<pair<int, int> > q;
  dist[si][sj] = 0;
  q.push(make_pair(si, sj));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 1 || nx > n || ny < 1 || ny > m || s[nx][ny] == '#') continue;
      if (dist[nx][ny] != -1) continue;
      dist[nx][ny] = dist[x][y] + 1;
      q.push(make_pair(nx, ny));
    }
  }
}
int main() {
  n = in();
  m = in();
  k = in();
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == 'S') {
        si = i;
        sj = j;
        s[i][j] = '.';
      }
    }
  }
  bfs();
  int MIN = INF;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dist[i][j] != -1 && dist[i][j] <= k) {
        if (min(min(i - 1, j - 1), min(n - i, n - j)) < MIN) {
          MIN = min(min(i - 1, j - 1), min(n - i, n - j));
        }
      }
    }
  }
  printf("%d\n", (MIN + k - 1) / k + 1);
  return 0;
}
