#include <bits/stdc++.h>
using namespace std;
const long long INF = INT_MAX;
const long long MOD = 998244353;
int di[4] = {0, 1, 0, -1};
int dj[4] = {1, 0, -1, 0};
int H, W, K;
bool visit[900][900];
int dfs(const vector<string>& A, int i, int j, int depth) {
  if (depth == K) {
    return min(min(i, H - i - 1), min(j, W - j - 1));
  }
  if (visit[i][j]) return INF;
  visit[i][j] = true;
  int ret = min(min(i, H - i - 1), min(j, W - j - 1));
  for (long long d = 0; (d) < (4); ++(d)) {
    int ni = i + di[d];
    int nj = j + dj[d];
    if (ni < 0 || H <= ni || nj < 0 || W <= nj || A[ni][nj] == '#') {
      continue;
    }
    ret = min(ret, dfs(A, ni, nj, depth + 1));
  }
  return ret;
}
int main() {
  cin >> H >> W >> K;
  vector<string> A(H);
  int si, sj;
  for (long long i = 0; (i) < (H); ++(i)) cin >> A[i];
  for (long long i = 0; (i) < (H); ++(i)) {
    for (long long j = 0; (j) < (W); ++(j)) {
      if (A[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  int rest = dfs(A, si, sj, 0);
  int ans = (rest ? (rest - 1) / K + 1 : 0) + 1;
  cout << ans << endl;
  return 0;
}
