#include <bits/stdc++.h>
using namespace std;
int N, M, K;
char A[800][800];
bool rc[800][800];
vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};
void dfs(int d, int x, int y) {
  rc[x][y] = true;
  if (d > 0) {
    rc[x][y] = true;
    for (int(k) = 0; (k) < (4); (k)++) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (0 <= nx && nx < N && 0 <= ny && ny < M)
        if (A[nx][ny] != '#' && !rc[nx][ny]) dfs(d - 1, nx, ny);
    }
  }
}
int main() {
  cin >> N >> M >> K;
  int sx, sy;
  for (int(i) = 0; (i) < (N); (i)++)
    for (int(j) = 0; (j) < (M); (j)++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        sx = i;
        sy = j;
        A[i][j] = '.';
      }
    }
  dfs(K, sx, sy);
  int step = 1000;
  for (int(i) = 0; (i) < (N); (i)++)
    for (int(j) = 0; (j) < (M); (j)++)
      if (rc[i][j]) {
        int tx = min(i, N - 1 - i);
        int ty = min(j, M - 1 - j);
        int tmp = min(tx, ty);
        step = min(step, tmp);
      }
  int ans;
  if (step % K == 0)
    ans = 1 + step / K;
  else
    ans = 2 + step / K;
  cout << ans << endl;
}
