#include <bits/stdc++.h>
using namespace std;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
char c[1000][1000];
long long d[1000][1000];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  long long h, w, k;
  cin >> h >> w >> k;
  pair<long long, long long> s;
  for (long long i = (long long)0; i < (long long)h; i++) {
    cin >> c[i];
    for (long long j = (long long)0; j < (long long)w; j++) {
      if (c[i][j] == 'S') s = pair<long long, long long>(i, j);
    }
  }
  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] = LLONG_MAX;
    }
  }
  d[s.first][s.second] = 0;
  queue<pair<long long, long long> > q;
  q.push(s);
  while (q.size()) {
    pair<long long, long long> p = q.front();
    q.pop();
    for (long long k = (long long)0; k < (long long)4; k++) {
      long long y = p.first + vy[k], x = p.second + vx[k];
      if (!(0 <= x && x < w && 0 <= y && y < h)) continue;
      if (c[y][x] != '#' && d[y][x] > d[p.first][p.second]) {
        d[y][x] = d[p.first][p.second] + 1;
        q.push(pair<long long, long long>(y, x));
      }
    }
  }
  long long ans = LLONG_MAX;
  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 ((i == 0 || i == h - 1 || j == 0 || j == w - 1) && d[i][j] <= k) {
        ans = 1;
      } else if (d[i][j] <= k) {
        long long res = (i + k - 1) / k;
        res = min(res, (j + k - 1) / k);
        res = min(res, (h - i - 1 + k - 1) / k);
        res = min(res, (w - j - 1 + k - 1) / k);
        ans = min(ans, res + 1);
      }
    }
  }
  cout << ans << endl;
}
