#include <bits/stdc++.h>
using namespace std;
inline int Read() {
  int x = 0, f = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 805;
const int MAXM = 640005;
const int INF = 0x3f3f3f3f;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int n, m, k, ql, qr, ans = INF, qx[MAXM], qy[MAXM], dis[MAXN][MAXN];
char s[MAXN][MAXN];
int main() {
  memset(dis, INF, sizeof dis), n = Read(), m = Read(), k = Read();
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[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 i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (!nx || !ny || nx == n + 1 || ny == m + 1) continue;
      if (s[nx][ny] == '.' && dis[nx][ny] == INF)
        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))) - 1) / k + 2);
  return printf("%d\n", ans), 0;
}
