#include <bits/stdc++.h>
using namespace std;
const int N = 805;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int dis[N][N];
char a[N][N];
queue<pair<int, int>> q;
int main() {
  int n, m, k, sx, sy;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  memset(dis, -1, sizeof(dis));
  dis[sx][sy] = 0;
  q.push(make_pair(sx, sy));
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int kx = p.first, ky = p.second;
    if (dis[kx][ky] == k + 1) break;
    for (int i = 0; i < 4; i++) {
      int x = kx + dx[i], y = ky + dy[i];
      if (x < 1 || x > n || y < 1 || y > m || a[x][y] == '#') continue;
      if (dis[x][y] == -1) {
        dis[x][y] = dis[kx][ky] + 1;
        q.push(make_pair(x, y));
      }
    }
  }
  while (!q.empty()) q.pop();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (dis[i][j] <= k && dis[i][j] != -1) q.push(make_pair(i, j));
  int ans = INT_MAX;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    ans = min(ans, (min(min(n - x, x), min(y, m - y)) + k - 1) / k);
  }
  printf("%d\n", ans + 1);
  return 0;
}
