#include <bits/stdc++.h>
using namespace std;
int h, w, k;
int a[1000][1000];
int b[1000][1000];
string d;
int sx, sy;
int ans;
const int dx[]{0, 0, 1, -1};
const int dy[]{1, -1, 0, 0};
void bfs(int xx, int yy, int step) {
  if (step >= k) return;
  for (int i = 0; i < 4; i++) {
    int nx = xx + dx[i];
    int ny = yy + dy[i];
    if (nx < 1 || nx > h || ny < 1 || ny > w) continue;
    if (a[nx][ny] == '.' && !b[nx][ny]) {
      b[nx][ny] = 1;
      bfs(nx, ny, step + 1);
    }
  }
}
int main() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    cin >> d;
    for (int o = 1; o <= w; o++) {
      a[i][o] = d[o - 1];
      if (a[i][o] == 'S') sx = i, sy = o;
    }
  }
  b[sx][sy] = 1;
  ans = 0x7fffffff;
  if (sx == 1 || sx == h || sy == 1 || sy == w) return puts("0") * 0;
  bfs(sx, sy, 0);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      if (b[i][j]) {
        int t = min(min(i - 1, j - 1), min(h - i, w - j));
        int cur = (t + k - 1) / k;
        ans = min(ans, cur + 1);
      }
  cout << ans;
}
