#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7, INF = 1e18;
template <class T>
inline bool CHMAX(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool CHMIN(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
long long dx[] = {1, -1, 0, 0};
long long dy[] = {0, 0, 1, -1};
signed main() {
  long long H, W, K;
  cin >> H >> W >> K;
  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 = j, sy = i;
      }
  queue<pair<pair<long long, long long>, long long>> que;
  vector<vector<long long>> dist(H, vector<long long>(W, INF));
  dist[sy][sx] = 0;
  que.push({{sy, sx}, K});
  long long mi = min({sy, sx, H - sy - 1, W - sx - 1});
  while (!que.empty()) {
    long long i = que.front().first.first;
    long long j = que.front().first.second;
    long long cost = que.front().second;
    que.pop();
    if (cost == 0) continue;
    for (long long k = (0); k < (4); k++) {
      long long ni = i + dx[k];
      long long nj = j + dy[k];
      if (!(0 <= ni && ni < H) || !(0 <= nj && nj < W)) continue;
      if (s[ni][nj] == '#') continue;
      que.push({{ni, nj}, cost - 1});
      CHMIN(mi, min({ni, nj, H - ni - 1, W - nj - 1}));
    }
  }
  cout << 1 + (mi + K - 1) / K << endl;
}
