#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chmin(T &a, T b) {
  a = min(a, b);
}
template <class T>
inline void chmax(T &a, T b) {
  a = max(a, b);
}
long long H, W, K;
char c[805][805];
long long sx, sy;
long long dis[805][805];
signed main() {
  cin >> H >> W >> K;
  for (long long i = 0; i < H; i++)
    for (long long j = 0; j < W; j++) {
      cin >> c[i][j];
      dis[i][j] = (long long)(3e18);
      if (c[i][j] == 'S') {
        sx = i, sy = j;
        dis[i][j] = 0;
      }
    }
  queue<pair<long long, long long> > que;
  que.push({sx, sy});
  long long dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  while (!que.empty()) {
    pair<long long, long long> p = que.front();
    que.pop();
    if (dis[p.first][p.second] == K) break;
    for (long long i = 0; i < 4; i++) {
      long long nx = dx[i] + p.first, ny = dy[i] + p.second;
      if (nx < 0 || ny < 0 || nx >= H || ny >= W || c[nx][ny] == '#') continue;
      if (dis[nx][ny] == (long long)(3e18)) {
        dis[nx][ny] = dis[p.first][p.second] + 1;
        que.push({nx, ny});
      }
    }
  }
  long long ans = (long long)(3e18);
  for (long long i = 0; i < H; i++)
    for (long long j = 0; j < W; j++) {
      if (dis[i][j] != (long long)(3e18)) {
        long long mi = min({i, H - i + 1, j, W - j + 1});
        chmin(ans, 1 + (mi + K - 1) / K);
      }
    }
  cout << ans << endl;
}
