#include <bits/stdc++.h>
using namespace std;
template <class T>
void chmin(T& x, T y) {
  if (x > y) x = y;
}
int ans = 1 << 30;
char bld[810][810];
bool came[810][810];
int H, W, K, s, t;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
void dfs(int a, int b, int cost) {
  if (came[a][b]) return;
  came[a][b] = true;
  if (cost > K) return;
  if (bld[a][b] == '#') return;
  if (a == 1 || b == 1 || a == H || b == W) {
    ans = 1;
    return;
  }
  chmin(ans, ((H - a - 1) / K) + 2);
  chmin(ans, ((a - 2) / K) + 2);
  chmin(ans, ((W - b - 1) / K) + 2);
  chmin(ans, ((b - 2) / K) + 2);
  for (int i = 0; i < 4; i++) {
    int ux = a + dx[i];
    int uy = b + dy[i];
    if (1 <= ux && ux <= H && 1 <= uy && uy <= W) {
      dfs(ux, uy, cost + 1);
    }
  }
}
int main() {
  cin >> H >> W >> K;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> bld[i][j];
      if (bld[i][j] == 'S') s = i, t = j;
    }
  }
  dfs(s, t, 0);
  cout << ans << endl;
  return 0;
}
