#include <bits/stdc++.h>
using namespace std;
const long long MOD = pow(10, 9) + 7;
const long long LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;
int H, W, K;
vector<string> A;
vector<vector<bool>> ok;
void dfs(int sh, int sw, int K) {
  ok[sh][sw] = true;
  if (K) {
    K--;
    if (sh != H - 1 && A[sh + 1][sw] != '#' && !ok[sh + 1][sw])
      dfs(sh + 1, sw, K);
    if (sh != 0 && A[sh - 1][sw] != '#' && !ok[sh - 1][sw]) dfs(sh - 1, sw, K);
    if (sw != W - 1 && A[sh][sw + 1] != '#' && !ok[sh][sw + 1])
      dfs(sh, sw + 1, K);
    if (sw != 0 && A[sh][sw - 1] != '#' && !ok[sh][sw - 1]) dfs(sh, sw - 1, K);
  }
}
int main() {
  cin >> H >> W >> K;
  A.resize(H);
  for (int i = 0; i < (int)H; i++) cin >> A[i];
  ok.resize(H, vector<bool>(W, false));
  int sh, sw;
  for (int i = 0; i < (int)H; i++)
    for (int j = 0; j < (int)W; j++)
      if (A[i][j] == 'S') {
        sh = i;
        sw = j;
      }
  dfs(sh, sw, K);
  int d = H + W;
  for (int i = 0; i < (int)H; i++)
    for (int j = 0; j < (int)W; j++) {
      if (ok[i][j]) {
        int dh = min(i, (H - 1) - i);
        int dw = min(j, (W - 1) - j);
        d = min(d, min(dh, dw));
      }
    }
  int result = (d + K - 1) / K + 1;
  cout << result << endl;
  return 0;
}
