#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = acos(-1.0);
int64_t isGoal(int64_t hnow, int64_t wnow, int64_t H, int64_t W) {
  int64_t ans = (std::numeric_limits<int64_t>::max() / 4);
  ans = min(ans, hnow);
  ans = min(ans, wnow);
  ans = min(ans, H - hnow - 1);
  ans = min(ans, W - wnow - 1);
  return ans;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int64_t H, W, K;
  cin >> H >> W >> K;
  vector<vector<int64_t>> A(H, vector<int64_t>(W, 0));
  int64_t sh, sw;
  for (int64_t i = (int64_t)(0); i < (int64_t)(H); i++) {
    string s;
    cin >> s;
    for (int64_t j = (int64_t)(0); j < (int64_t)(W); j++) {
      A[i][j] = (s[j] == '#' ? 1 : 0);
      if (s[j] == 'S') {
        sh = i;
        sw = j;
      }
    }
  }
  const int64_t D[5] = {0, 1, 0, -1, 0};
  vector<vector<int64_t>> searched(H, vector<int64_t>(W, 0));
  stack<array<int64_t, 3>> dfs1;
  dfs1.push({sh, sw, 0});
  int64_t ans = (std::numeric_limits<int64_t>::max() / 4);
  while (!dfs1.empty()) {
    auto x = dfs1.top();
    dfs1.pop();
    int64_t hpos = x[0];
    int64_t wpos = x[1];
    ans = min(ans, isGoal(hpos, wpos, H, W));
    if (ans == 0) {
      cout << 1 << endl;
      return 0;
    }
    if (searched[hpos][wpos]++) continue;
    if (K <= x[2]) continue;
    for (int64_t d = (int64_t)(0); d < (int64_t)(4); d++) {
      int64_t hnew = hpos + D[d];
      int64_t wnew = wpos + D[d + 1];
      if (A[hnew][wnew]) continue;
      dfs1.push({hnew, wnew, x[2] + 1});
    }
  }
  cout << (2 + ((ans - 1) / K)) << endl;
  return 0;
}
