#include <bits/stdc++.h>
using namespace std;
int x[4] = {-1, 1, 0, 0};
int y[4] = {0, 0, 1, -1};
int H, W, K, inf = 10000000;
int checked[800][800] = {0};
int map[800][800];
int start[2];
char s;
void dfs(int a, int b, int d) {
  checked[a][b] = d;
  inf = fmin(inf, fmin(fmin(a, H - 1 - a), fmin(b, W - 1 - b)));
  if (d < K) {
    for (int i = 0; i < 4; i++) {
      int nx = a + x[i];
      int ny = b + y[i];
      if (map[nx][ny] && (checked[nx][ny] == 0 || checked[nx][ny] > d) &&
          nx > -1 && nx < H && ny < W && ny > -1) {
        dfs(nx, ny, d + 1);
      }
    }
  }
}
int main(void) {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> s;
      if (s == '#') map[i][j] = 0;
      if (s == 'S') {
        map[i][j] = 1;
        start[0] = i;
        start[1] = j;
      }
      if (s == '.') map[i][j] = 1;
    }
  }
  dfs(start[0], start[1], 0);
  cout << (inf + K - 1) / K + 1;
}
