#include <bits/stdc++.h>
using namespace std;
long long dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
long long dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
long long H, W, K;
char A[800][800];
bool dp[800][800] = {};
void dfs(long long y, long long x, long long depth) {
  dp[y][x] = true;
  if (depth == K) {
    return;
  }
  for (long long i = 0; i < 4; i++) {
    long long ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || ny >= H || nx < 0 || nx >= W || A[ny][nx] == '#' ||
        dp[ny][nx]) {
      continue;
    }
    dfs(ny, nx, depth + 1);
  }
}
signed main() {
  cin >> H >> W >> K;
  long long sy, sx;
  for (long long i = 0; i < H; i++) {
    string Al;
    cin >> Al;
    for (long long j = 0; j < W; j++) {
      A[i][j] = Al[j];
      if (A[i][j] == 'S') {
        sy = i, sx = j;
      }
    }
  }
  dfs(sy, sx, 0);
  long long ans = LLONG_MAX;
  for (long long i = 0; i < H; i++) {
    for (long long j = 0; j < W; j++) {
      if (dp[i][j]) {
        if (i == 0 || i == H - 1 || j == 0 || j == W - 1) {
          ans = 1;
        }
        ans = min(ans, 1 + (long long)ceil((double)i / K));
        ans = min(ans, 1 + (long long)ceil((double)j / K));
        ans = min(ans, 1 + (long long)ceil((double)(H - i - 1) / K));
        ans = min(ans, 1 + (long long)ceil((double)(W - j - 1) / K));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
