#include <bits/stdc++.h>
using namespace std;
int h, w, k, d[800][800], v = 1e9, dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};
string a[800];
bool b[800][800];
void f(int y, int x, int z) {
  if (!b[y][x] && z <= k) {
    v = min(v, min(min(y, h - 1 - y), min(x, w - 1 - x)));
    b[y][x] = true;
    d[y][x] = z;
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w) {
        if (a[ny][nx] == '.') f(ny, nx, z + 1);
      }
    }
  }
}
int main(void) {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    cin >> a[i];
    fill(d[i], d[i] + w, 1e9);
    fill(b[i], b[i] + w, false);
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        f(i, j, 0);
        int ans = (v + k - 1) / k + 1;
        cout << ans << endl;
        return 0;
      }
    }
  }
  return 0;
}
