#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
char A[810][810];
int B[810][810] = {{0}}, visited[810][810] = {0};
bool in(int x, int y) { return 0 <= x && x <= H - 1 && 0 <= y && y <= W - 1; }
void dfs(int x, int y, int k) {
  visited[x][y] = 1;
  B[x][y] = 1;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (in(nx, ny) && visited[nx][ny] == 0 && k < K && A[nx][ny] == '.') {
      dfs(nx, ny, k + 1);
    }
  }
}
int main() {
  cin >> H >> W >> K;
  int sx, sy;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  dfs(sx, sy, 0);
  int ans = 1e9;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (B[i][j] == 1) {
        int t = min(min(i, H - 1 - i), min(j, W - j - 1));
        ans = min(ans, 1 + (t + K - 1) / K);
        if (i == 0 || i == H - 1 || j == 0 || j == W - 1) ans = 1;
      }
    }
  }
  cout << ans << endl;
}
