#include <bits/stdc++.h>
using namespace std;
const int N = 805, M = 640005;
char s[N][N];
int n, m, K, ans, dis[N][N], ql, qr;
pair<int, int> qu[M];
void bfs(int ux, int uy) {
  memset(dis, -1, sizeof(dis));
  qu[ql = qr = 1] = {ux, uy};
  dis[ux][uy] = 0;
  while (ql <= qr && dis[qu[ql].first][qu[ql].second] <= K) {
    int x = qu[ql].first, y = qu[ql].second;
    for (int dx : {-1, 0, 1})
      for (int dy : {dx ? 0 : -1, dx ? 0 : 1})
        if (s[x + dx][y + dy] == '.' && !~dis[x + dx][y + dy])
          dis[x + dx][y + dy] = dis[x][y] + 1, qu[++qr] = {x + dx, y + dy};
    ql++;
    ans = min(min(x - 1, n - x), min(y - 1, m - y));
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &K);
  ans = min(n, m);
  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') {
        bfs(i, j);
        break;
      }
  printf("%d\n", 1 + (ans + K - 1) / K);
  return 0;
}
