#include <bits/stdc++.h>
using namespace std;
int H, W, K, res = 1000;
char A[800][800];
int used[800][800];
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
void dfs(int x, int y) {
  used[x][y] = 1;
  for (int i = 0; i < 4; i++) {
    int tx = x + dx[i], ty = y + dy[i];
    if (tx >= 0 && tx < H && ty >= 0 && ty < W && used[tx][ty] == 0 &&
        A[tx][ty] != '#') {
      dfs(tx, ty);
    }
  }
  if (x == 0 || x == H - 1 || y == 0 || y == W - 1) {
    res = 0;
  } else {
    res = min(res, min(x, min(y, min(H - x - 1, W - y - 1))));
  }
}
int main() {
  int sx, sy;
  cin >> H >> W >> K;
  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);
  cout << ((res + K - 1) / K + 1) << endl;
  return 0;
}
