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