#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int H, W, K;
string A[802];
int dist(int y, int x) {
  int ret = min(y, x);
  ret = min(ret, H - y);
  ret = min(ret, W - x);
  return ret;
}
using P = pair<int, int>;
vector<P> getNear(P s) {
  queue<P> que;
  que.push(s);
  map<P, int> dp;
  dp[s] = 0;
  vector<P> ps;
  ps.push_back(s);
  while (que.size()) {
    auto now = que.front();
    que.pop();
    if (dp[now] > K) continue;
    for (int k = 0; k < (int)(4); k++) {
      auto next = now;
      next.first += dy[k];
      next.second += dx[k];
      if (next.first < 0 || next.second < 0) continue;
      if (next.first >= H || next.second >= W) continue;
      if (dp.count(next)) continue;
      if (A[next.first][next.second] == '#') continue;
      dp[next] = dp[now] + 1;
      que.push(next);
      ps.push_back(next);
    }
  }
  return ps;
}
int INF = 1e9;
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < (int)(H); i++) cin >> A[i];
  int ans = INF;
  P s;
  for (int i = 0; i < (int)(H); i++)
    for (int j = 0; j < (int)(W); j++) {
      if (A[i][j] == 'S') {
        s.first = i;
        s.second = j;
      }
    }
  auto ps = getNear(s);
  for (auto p : ps) {
    ans = min(ans, dist(p.first, p.second));
  }
  ans = ceil(1.0 * ans / K);
  ans++;
  cout << ans << endl;
  return 0;
}
