#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int n, m, K, a[810][810], vis[810][810], sx, sy;
queue<int> x, y, k;
int main() {
  scanf("%d%d%d", &n, &m, &K);
  memset(a, 0, sizeof(a));
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    ;
    for (int j = 0; j < m; j++) {
      if (s[j] == 'S') sx = i, sy = j + 1;
      a[i][j + 1] = (s[j] == '.');
    }
  }
  vis[sx][sy] = 1;
  if (sx == 1 || sx == n || sy == 1 || sy == m) return puts("0");
  x.push(sx);
  y.push(sy);
  k.push(0);
  while (!x.empty()) {
    int xx, yy, kk;
    xx = x.front();
    x.pop();
    yy = y.front();
    y.pop();
    kk = k.front();
    k.pop();
    if (kk == K) continue;
    if (xx == 1 || xx == n || yy == 1 || yy == m) return puts("1");
    for (int i = 0; i < 4; i++) {
      int nx, ny;
      nx = xx + dx[i];
      ny = yy + dy[i];
      if (a[nx][ny] && !vis[nx][ny]) {
        vis[nx][ny] = 1;
        x.push(nx);
        y.push(ny);
        k.push(kk + 1);
      }
    }
  }
  int res = (int)1e9 + 7;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (vis[i][j]) {
        int d = min(min(i - 1, j - 1), min(n - i, m - j));
        int cur = (d + K - 1) / K;
        res = min(res, cur + 1);
      }
  cout << res;
  return 0;
}
