#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
long long H, W, K;
long long dx[] = {0, 0, -1, 1};
long long dy[] = {-1, 1, 0, 0};
long long check(long long a, long long b) {
  long long res = 1000000000;
  chmin(res, a);
  chmin(res, b);
  chmin(res, H - 1 - a);
  chmin(res, W - 1 - b);
  return res;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W >> K;
  vector<string> S(H);
  for (long long i = 0; i < H; i++) cin >> S[i];
  long long sx, sy;
  for (long long i = 0; i < H; i++)
    for (long long j = 0; j < W; j++)
      if (S[i][j] == 'S') {
        sx = i, sy = j;
      }
  long long ans = 1000000000;
  bool vis[810][810] = {false};
  long long dis[810][810];
  for (long long i = 0; i < 810; i++)
    for (long long j = 0; j < 810; j++) dis[i][j] = 1000000000;
  queue<pair<long long, long long> > Q;
  Q.push(pair<long long, long long>(sx, sy));
  vis[sx][sy] = true;
  dis[sx][sy] = 0;
  while (!Q.empty()) {
    long long x = Q.front().first, y = Q.front().second;
    Q.pop();
    for (long long k = 0; k < 4; k++) {
      long long nx = x + dx[k], ny = y + dy[k];
      if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
        if (S[nx][ny] != '#' && dis[x][y] + 1 <= K &&
            dis[x][y] + 1 < dis[nx][ny]) {
          dis[nx][ny] = dis[x][y] + 1;
          vis[nx][ny] = true;
          Q.push(pair<long long, long long>(nx, ny));
        }
      }
    }
  }
  for (long long i = 0; i < H; i++) {
    for (long long j = 0; j < W; j++) {
      if (dis[i][j] <= K) {
        long long tmp = check(i, j);
        if (tmp != 0) tmp = (tmp - 1) / K + 2;
        chmin(ans, tmp);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
