#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int q[1234567][2], lb, ub, d[810][810];
int n, m, k, sx, sy, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
string s[810];
void fun(int x, int y, int now) {
  if (d[x][y] <= now || s[x][y] != '.') return;
  d[x][y] = now;
  ub++;
  q[ub][0] = x;
  q[ub][1] = y;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == 'S') sx = i, sy = j, s[i][j] = '.';
      d[i][j] = 1e9;
    }
  }
  fun(sx, sy, 0);
  int x, y;
  while (lb < ub) {
    lb++;
    x = q[lb][0];
    y = q[lb][1];
    for (int i = 0; i < 4; i++) fun(x + dx[i], y + dy[i], d[x][y] + 1);
  }
  int ans = 1e9;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (d[i][j] <= k) {
        ans = min(ans,
                  (min(min(i - 1, n - i), min(j - 1, m - j)) + k - 1) / k + 1);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
