#include <bits/stdc++.h>
using namespace std;
struct S {
  int x, y;
};
int H, W, K;
char B[800][800] = {};
void printArray(char A[], int N) {
  for (int i = 0; i < N; i++) {
    cout << A[i];
    if (i != N - 1) {
      cout << ' ';
    }
  }
  cout << endl;
}
static const int INFTY = (1 << 21);
static const int N = 800;
int d[N][N];
int bfs(S start) {
  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  queue<S> q;
  q.push(start);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = INFTY;
    }
  }
  d[start.x][start.y] = 0;
  S u;
  int ans = INFTY;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    if (d[u.x][u.y] >= K) {
      continue;
    }
    int mind = min(u.x, min(u.y, min(W - 1 - u.x, H - 1 - u.y)));
    ans = min(ans, (mind + K - 1) / K + 1);
    for (int i = 0; i < 4; i++) {
      int nx = u.x + dx[i];
      int ny = u.y + dy[i];
      if (B[nx][nx] == '#') {
        continue;
      }
      if (d[nx][nx] != INFTY) {
        continue;
      }
      d[nx][ny] = d[u.x][u.y] + 1;
      S s = {nx, ny};
      q.push(s);
    }
  }
  return ans;
}
int main() {
  S start;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> B[i][j];
      if (B[i][j] == 'S') {
        start.x = i;
        start.y = j;
      }
    }
  }
  cout << bfs(start) << endl;
  return 0;
}
