#include <bits/stdc++.h>
using namespace std;
const int inf = ~0u >> 1, MOD = 1e9 + 7;
inline int rd() {
  int x, c, f = 1;
  while (!isdigit(c = getchar())) f = c != '-';
  x = c - '0';
  while (isdigit(c = getchar())) x = x * 10 + c - '0';
  return f ? x : -x;
}
const int N = 811, dr[] = {0, 0, 1, -1}, dc[] = {1, -1, 0, 0};
char s[N][N];
int H, W, K, qx[N * N], qy[N * N], f[N][N];
inline void bfs(int Sx, int Sy) {
  if (Sx == 1 || Sx == H || Sy == 1 || Sy == W) {
    cout << 0 << endl;
    return;
  }
  memset(f, -1, sizeof f);
  int h = 0, t = 1, x, y;
  qx[h] = Sx, qy[h] = Sy;
  f[Sx][Sy] = 0;
  while (h < t) {
    x = qx[h], y = qy[h], ++h;
    for (int i = (0), i_end = (4); i < i_end; ++i) {
      int nx = x + dr[i], ny = y + dc[i];
      if (nx < 1 || nx > H || ny < 1 || ny > W || s[nx][ny] == '#' ||
          ~f[nx][ny])
        continue;
      f[nx][ny] = f[x][y] + 1;
      if (f[x][y] < K) qx[t] = nx, qy[t] = ny, ++t;
    }
  }
  int ans = inf;
  for (int i = (1), i_end = (H); i <= i_end; ++i)
    for (int j = (1), j_end = (W); j <= j_end; ++j)
      if (~f[i][j]) {
        int p = min(min(i - 1, j - 1), min(H - i, W - j));
        ans = min(ans, 1 + p / K + (p % K != 0));
      }
  cout << ans << endl;
}
int main() {
  H = rd(), W = rd(), K = rd();
  for (int i = (1), i_end = (H); i <= i_end; ++i) scanf("%s", s[i] + 1);
  for (int i = (1), i_end = (H); i <= i_end; ++i)
    for (int j = (1), j_end = (W); j <= j_end; ++j)
      if (s[i][j] == 'S') {
        bfs(i, j);
        break;
      }
}
