#include <bits/stdc++.h>
using namespace std;
string a[1000];
long long d[1000][1000], dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
signed main(void) {
  long long h, w, k;
  cin >> h >> w >> k;
  for (long long i = (long long)0; i < (long long)h; ++i) cin >> a[i];
  long long sx, sy;
  queue<pair<long long, long long> > que;
  for (long long i = (long long)0; i < (long long)h; ++i)
    for (long long j = (long long)0; j < (long long)w; ++j) {
      d[i][j] = (1LL << 30);
      if (a[i][j] == 'S') {
        sx = j;
        sy = i;
      }
    }
  que.push({sx, sy});
  d[sx][sy] = 0;
  long long ans = (min({sx, sy, h - sy - 1, w - sx - 1}) + k - 1) / k + 1;
  while (que.size()) {
    pair<long long, long long> p = que.front();
    que.pop();
    long long x = p.first, y = p.second;
    if (p.first == 0 || p.first == w - 1 || p.second == 0 ||
        p.second == h - 1 || d[p.second][p.first] == k)
      continue;
    ans = min(ans, (min({x, y, h - y - 1, w - x - 1}) + k - 1) / k + 1);
    for (long long i = (long long)0; i < (long long)4; ++i) {
      long long nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < w && 0 <= ny && ny < h && a[ny][nx] != '#' &&
          d[ny][nx] == (1LL << 30)) {
        que.push(pair<long long, long long>(nx, ny));
        d[ny][nx] = d[p.first][p.second] + 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
