#include <bits/stdc++.h>
using namespace std;
int cx[] = {-1, 1, 0, 0};
int cy[] = {0, 0, -1, 1};
int N, M, K;
char a[805][805];
int d[805][805];
queue<pair<int, int> > q;
int main() {
  scanf("%d%d%d\n", &N, &M, &K);
  for (int i = 1; i <= N; i++) scanf("%s", a[i] + 1);
  int x = 0, y = 0;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++)
      if (a[i][j] == 'S') {
        x = i, y = j;
        a[i][j] = '.';
      }
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++) d[i][j] = 1 << 30;
  q.push({x, y});
  d[x][y] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + cx[i];
      int ny = y + cy[i];
      if (nx < 1 || ny < 1 || nx > N || ny > M) continue;
      if (a[nx][ny] == '#') continue;
      if (d[nx][ny] > d[x][y] + 1) {
        d[nx][ny] = d[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
  int ans = 1 << 30;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++)
      if (d[i][j] <= K) {
        int dst = min(min(i - 1, j - 1), min(N - i, M - i));
        ans = min(ans, 1 + (dst + K - 1) / K);
      }
  printf("%d\n", ans);
  return 0;
}
