#include <bits/stdc++.h>
using namespace std;
char A[802][802];
int dp[802][802];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main(void) {
  int i, j, H, W, K, sx, sy;
  scanf("%d%d%d", &H, &W, &K);
  for (i = 0; i < H; i++) scanf("%s", &A[i]);
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        sx = j;
        sy = i;
      }
    }
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++) {
      dp[i][j] = -1;
    }
  dp[sy][sx] = 0;
  queue<pair<int, int> > P;
  P.push(make_pair(sx, sy));
  while (!P.empty()) {
    int x = P.front().first;
    int y = P.front().second;
    P.pop();
    for (i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= W || ny < 0 || ny >= H) {
        if (dp[y][x] <= K) return !printf("1\n");
        continue;
      }
      if (~dp[ny][nx] || A[ny][nx] == '#') continue;
      dp[ny][nx] = dp[y][x] + 1;
      P.push(make_pair(nx, ny));
    }
  }
  int ret = 1e9;
  for (i = 0; i < H; i++)
    for (j = 0; j < H; j++) {
      if (~dp[i][j] && dp[i][j] <= K) {
        int d = min(min(i, j), min(H - i - 1, W - i - 1));
        ret = min(ret, (d + K - 1) / K);
      }
    }
  return !printf("%d\n", ret + 1);
}
