#include <bits/stdc++.h>
using namespace std;
long long MOD = (long long)1000000000 + 7;
const long long INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;
const long long MAX = 200020;
long long dx[4] = {0, 0, 1, -1};
long long dy[4] = {1, -1, 0, 0};
bool visited[1000][1000];
char A[1000][1000];
long long H, W, K;
long long dfs(long long x, long long y, long long n) {
  visited[x][y] = true;
  if (n == 0) {
    return min({x, H - x + 1, y, W - y + 1});
  }
  long long res = min({x, H - x + 1, y, W - y + 1});
  for (long long i = 0; i < 4; i++) {
    long long nx = x + dx[i];
    long long ny = y + dy[i];
    if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
      if (visited[nx][ny]) continue;
      if (A[nx][ny] != '#') {
        res = min(res, dfs(nx, ny, n - 1));
      }
    }
  }
  return res;
}
void solve() {
  cin >> H >> W >> K;
  long long sx, sy;
  for (long long i = 0; i < H; i++) {
    for (long long j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        sx = i, sy = j;
      }
    }
  }
  long long rem = dfs(sx, sy, K);
  long long ans = 1;
  ans += (rem + K - 1) / K;
  cout << ans << endl;
  ;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}
