#include <bits/stdc++.h>
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int MN = 805;
int N, M, K, sx, sy;
char A[MN][MN];
int qx[MN * MN], qy[MN * MN], ql, qr;
int dis[MN][MN];
int Ans;
int main() {
  scanf("%d%d%d", &N, &M, &K), Ans = N;
  for (int i = 1; i <= N; ++i) {
    scanf("%s", A[i] + 1);
    for (int j = 1; j <= M; ++j) {
      dis[i][j] = -1;
      if (A[i][j] == 'S') sx = i, sy = j;
    }
  }
  dis[sx][sy] = 0, ql = qr = 1, qx[1] = sx, qy[1] = sy;
  while (ql <= qr) {
    int x = qx[ql], y = qy[ql];
    ++ql;
    int di = std::min({x - 1, N - x, y - 1, N - y});
    Ans = std::min(Ans, 1 + (di + K - 1) / K);
    if (dis[x][y] == K) continue;
    for (int d = 0; d < 4; ++d) {
      int nx = x + dx[d], ny = y + dy[d];
      if (nx < 1 || nx > N || ny < 1 || ny > M || A[nx][ny] == '#' ||
          ~dis[nx][ny])
        continue;
      dis[nx][ny] = dis[x][y] + 1;
      ++qr, qx[qr] = nx, qy[qr] = ny;
    }
  }
  printf("%d\n", Ans);
  return 0;
}
