#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int sc = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') sc = sc * 10 + ch - '0', ch = getchar();
  return sc * f;
}
const int MAXN = 805;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int n, m, dis[MAXN][MAXN], K, qx[MAXN * MAXN], qy[MAXN * MAXN], ql, qr, ans;
char ch[MAXN][MAXN];
int main() {
  memset(dis, ans = 0x3f, sizeof dis);
  n = read(), m = read(), K = read();
  for (int i = 1; i <= n; i++) scanf("%s", ch[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (ch[i][j] == 'S') qx[++qr] = i, qy[qr] = j, dis[i][j] = 0;
  while (ql ^ qr) {
    int x = qx[++ql], y = qy[ql];
    if (dis[x][y] >= K) break;
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (ch[nx][ny] == '.' && dis[nx][ny] == 0x3f3f3f3f)
        dis[nx][ny] = dis[x][y] + 1, qx[++qr] = nx, qy[qr] = ny;
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (dis[i][j] <= K)
        ans = min(
            ans, (min(i - 1, min(j - 1, min(n - i, m - j))) + (K - 1)) / K + 1);
  return printf("%d\n", ans), 0;
}
