#include <bits/stdc++.h>
namespace Big_Water_Problem {
inline bool check_min(int a, int &b) { return a < b ? b = a, 1 : 0; }
const int L = 810, INF = 1000000007;
const int mv[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
inline int got() {
  for (char c; c = getchar();)
    if (c == '.')
      return 1;
    else if (c == '#')
      return 0;
    else if (c == 'S')
      return 2;
}
int G[L][L], f[L][L];
int n, m, k, ans;
int sx, sy;
void initialize() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if ((G[i][j] = got()) == 2) sx = i, sy = j;
}
void play() {
  static int Q[L * L][2];
  int u = 0, d = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) f[i][j] = INF;
  f[Q[++u][0] = sx][Q[u][1] = sy] = 0;
  while (u > d) {
    int x = Q[++d][0], y = Q[d][1], tx, ty;
    for (int v = 0; v < 4; v++)
      if (G[tx = x + mv[v][0]][ty = y + mv[v][1]] &&
          check_min(f[x][y] + 1, f[tx][ty]))
        Q[++u][0] = tx, Q[u][1] = ty;
  }
}
int calc(int x, int y) {
  int ret = std::min(x - 1, n - x);
  check_min(std::min(y - 1, m - y), ret);
  return ret / k;
}
void solve() {
  initialize();
  play();
  int ans = INF;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (f[i][j] <= k) check_min(1 + calc(i, j), ans);
  printf("%d\n", ans);
}
}  // namespace Big_Water_Problem
int main() {
  Big_Water_Problem::solve();
  return 0;
}
