#include <bits/stdc++.h>
using namespace std;
long long int H, W, K;
string MAP[(800)];
int DIST[(800)][(800)];
bool is_inside(int i, int j) { return 0 <= i && i < H && 0 <= j && j < W; }
void solve() {
  int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  int r0, c0;
  queue<pair<int, int> > q1, q2;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      DIST[i][j] = (1000000000 + 7);
      if (MAP[i][j] == 'S') {
        r0 = i;
        c0 = j;
        q1.push(pair<int, int>(i, j));
        q2.push(pair<int, int>(i, j));
        DIST[i][j] = 0;
      }
    }
  }
  while (!q1.empty()) {
    pair<int, int> p = q1.front();
    q1.pop();
    int r = p.first, c = p.second;
    for (int i = 0; i < 4; i++) {
      int rx = r + dir[i][0], cx = c + dir[i][1];
      if (is_inside(rx, cx)) {
        int dist = DIST[r][c] + 1;
        if (MAP[rx][cx] == '.' && dist < DIST[rx][cx] && dist <= K) {
          DIST[rx][cx] = dist;
          q1.push(pair<int, int>(rx, cx));
          q2.push(pair<int, int>(rx, cx));
        }
      }
    }
  }
  long long int mn = (1000000000 + 7);
  while (!q2.empty()) {
    pair<int, int> p = q2.front();
    q2.pop();
    int r = p.first, c = p.second;
    long long int vals[4] = {r, c, W - 1 - r, H - 1 - c};
    for (int i = 0; i < 4; i++) mn = min(mn, vals[i]);
  }
  long long int ans = (mn + K - 1) / K + 1;
  cout << ans << endl;
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) cin >> MAP[i];
  solve();
  return 0;
}
