#include <bits/stdc++.h>
using namespace std;
int q[1010101][2], h, t, d[1010][1010];
int n, m, k, sx, sy;
char p[1010][1010];
void ins(int x, int y, int r) {
  if (d[x][y] <= r || p[x][y] != '.') return;
  d[x][y] = r;
  t++;
  q[t][0] = x;
  q[t][1] = y;
}
int main() {
  cin >> n >> m >> k;
  for (int i = (int)0; i < (int)n; i++) {
    cin >> p[i];
    for (int j = (int)0; j < (int)m; j++) {
      if (p[i][j] == 'S') sx = i, sy = j, p[i][j] = '.';
      d[i][j] = 1e9;
    }
  }
  ins(sx, sy, 0);
  while (h < t) {
    h++;
    int x = q[h][0] - 1, y = q[h][1] - 1;
    ins(x + 1, y, d[x][y] + 1);
    ins(x, y + 1, d[x][y] + 1);
    ins(x - 1, y, d[x][y] + 1);
    ins(x, y - 1, d[x][y] + 1);
  }
  int ans = 1e9;
  for (int i = (int)0; i < (int)n; i++)
    for (int j = (int)0; j < (int)m; j++)
      if (d[i][j] <= k)
        ans = min(ans,
                  1 + (min(min(i, n - i - 1), min(j, m - j - 1)) + k - 1) / k);
  cout << ans << endl;
  return 0;
}
