#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (long long i = 0; i < H; ++i) cin >> A[i];
  int sh, sw;
  for (long long i = 0; i < H; ++i)
    for (long long j = 0; j < W; ++j)
      if (A[i][j] == 'S') sh = i, sw = j;
  int dh[4] = {1, 0, -1, 0};
  int dw[4] = {0, 1, 0, -1};
  queue<pair<int, int>> que;
  que.push(pair<int, int>(sh, sw));
  vector<vector<int>> d(H, vector<int>(W, -1));
  d[sh][sw] = 0;
  int ans = INF;
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    int h = p.first, w = p.second;
    int x = (d[h][w] + min(h, min(H - 1 - h, min(w, W - 1 - w))) + K - 1) / K;
    if (d[h][w] < K)
      x = 1 + (min(h, min(H - 1 - h, min(w, W - 1 - w))) + K - 1) / K;
    chmin(ans, x);
    for (long long j = 0; j < 4; ++j) {
      int nh = h + dh[j], nw = w + dw[j];
      if (nh < 0 || nh >= W || nw < 0 || nw >= W) continue;
      if (A[nh][nw] == '#') continue;
      if (d[nh][nw] != -1) continue;
      d[nh][nw] = d[h][w] + 1;
      que.push(pair<int, int>(nh, nw));
    }
  }
  cout << ans << endl;
}
