#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long inf = 1ll << 61;
string s[806];
long long dist[806][805];
long long dist2[805][805];
long long dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
signed main() {
  long long h, w, k;
  cin >> h >> w >> k;
  for (long long i = 0; i < h; i++) cin >> s[i];
  long long sh, sw;
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++)
      if (s[i][j] == 'S') sh = i, sw = j;
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++) dist[i][j] = inf;
  dist[sh][sw] = 0;
  queue<pair<long long, long long> > Q;
  Q.push(pair<long long, long long>(sh, sw));
  while (Q.size()) {
    pair<long long, long long> p = Q.front();
    Q.pop();
    if (dist[p.first][p.second] == k) continue;
    for (long long i = 0; i < 4; i++) {
      long long nh = p.first + dx[i], nw = p.second + dy[i];
      if (0 <= nh && nh < h && 0 <= nw && nw < w && dist[nh][nw] == inf &&
          s[nh][nw] == '.') {
        dist[nh][nw] = dist[p.first][p.second] + 1;
        Q.push(pair<long long, long long>(nh, nw));
      }
    }
  }
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (dist[i][j] != inf) {
        Q.push(pair<long long, long long>(i, j));
        dist2[i][j] = 0;
      } else
        dist2[i][j] = inf;
    }
  }
  while (Q.size()) {
    pair<long long, long long> p = Q.front();
    Q.pop();
    if (dist2[p.first][p.second] == k) continue;
    for (long long i = 0; i < 4; i++) {
      long long nh = p.first + dx[i], nw = p.second + dy[i];
      if (0 <= nh && nh < h && 0 <= nw && nw < w && dist2[nh][nw] == inf) {
        dist2[nh][nw] = dist2[p.first][p.second] + 1;
        Q.push(pair<long long, long long>(nh, nw));
      }
    }
  }
  long long ans = inf;
  for (long long i = 0; i < h; i++) {
    ans = min(ans, (dist2[i][0] + k - 1) / k + 1);
    ans = min(ans, (dist2[i][w - 1] + k - 1) / k + 1);
  }
  for (long long i = 0; i < w; i++) {
    ans = min(ans, (dist2[0][i] + k - 1) / k + 1);
    ans = min(ans, (dist2[h - 1][i] + k - 1) / k + 1);
  }
  cout << ans << endl;
}
