#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
char a[N][N];
int d[N][N];
int n, m, k;
bool inside(int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; }
int dx[] = {-1, -1, 1, 1};
int dy[] = {-1, 1, -1, 1};
int div_up(int a, int b) { return (a + b - 1) / b; }
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int x = -1, y = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  memset(d, -1, sizeof d);
  d[x][y] = 0;
  queue<tuple<int, int>> q;
  q.push(make_tuple(x, y));
  while (!q.empty()) {
    int cx, cy;
    tie(cx, cy) = q.front();
    q.pop();
    if (d[cx][cy] == k) continue;
    for (int side = 0; side < 4; side++) {
      int nx = cx + dx[side];
      int ny = cy + dy[side];
      if (inside(nx, ny) && a[nx][ny] == '.' && d[nx][ny] == -1) {
        d[nx][ny] = d[cx][cy] + 1;
        q.push(make_tuple(nx, ny));
      }
    }
  }
  int ans = N * N;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (d[i][j] != -1) {
        int more = div_up(min(min(i, j), min(n - 1 - i, m - 1 - j)), k);
        ans = min(ans, more + 1);
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
