#include <bits/stdc++.h>
using namespace std;
const long long maxn = 800 + 5;
long long k, n, m;
long long h[maxn][maxn];
string a[maxn];
bool isvl(long long x, long long y) {
  return (x >= 0) and (x < n) and (y >= 0) and (y < m);
}
void dfs(long long x, long long y) {
  long long aa[4] = {0, 0, 1, -1}, b[4] = {1, -1, 0, 0};
  for (long long i = 0; i < 4; i++) {
    long long X = x + aa[i], Y = y + b[i];
    if (isvl(X, Y) and !h[X][Y] and h[x][y] <= k and a[X][Y] == '.') {
      h[X][Y] = h[x][y] + 1;
      dfs(X, Y);
    }
  }
  return;
}
int32_t main() {
  long long x, y;
  cin >> n >> m >> k;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    for (long long j = 0; j < m; j++) {
      if (a[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  long long ans = n;
  h[x][y] = 1;
  dfs(x, y);
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (h[i][j]) {
        long long z = (min(min(i, n - i - 1), min(j, m - j - 1)) + k - 1) / k;
        z++;
        (ans) = min((ans), (z));
      }
    }
  }
  cout << ans << endl;
}
