#include <bits/stdc++.h>
using namespace std;
vector<string> A;
int H, W, K, dist[820][820], dinf = 1e6;
queue<pair<int, int>> q = {};
void bfs() {
  while (q.size() > 0) {
    int s = q.front().first, t = q.front().second;
    q.pop();
    if (dist[s][t] >= K) break;
    if (s > 0) {
      if (dist[s - 1][t] == dinf && A[s - 1][t] == '.') {
        dist[s - 1][t] = dist[s][t] + 1;
        q.push(make_pair(s - 1, t));
      }
    }
    if (s + 1 < H) {
      if (dist[s + 1][t] == dinf && A[s + 1][t] == '.') {
        dist[s + 1][t] = dist[s][t] + 1;
        q.push(make_pair(s + 1, t));
      }
    }
    if (t > 0) {
      if (dist[s][t - 1] == dinf && A[s][t - 1] == '.') {
        dist[s][t - 1] = dist[s][t] + 1;
        q.push(make_pair(s, t - 1));
      }
    }
    if (t + 1 < W) {
      if (dist[s][t + 1] == dinf && A[s][t + 1] == '.') {
        dist[s][t + 1] = dist[s][t] + 1;
        q.push(make_pair(s, t + 1));
      }
    }
  }
}
int main() {
  cin >> H >> W >> K;
  A.resize(H);
  for (int i = 0; i < H; ++i) {
    cin >> A[i];
    for (int j = 0; j < W; ++j) {
      if (A[i][j] == 'S') {
        dist[i][j] = 0;
        q.push(make_pair(i, j));
      } else
        dist[i][j] = dinf;
    }
  }
  bfs();
  int ans = dinf;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (dist[i][j] <= K) {
        int mn = min({i, H - 1 - i, j, W - 1 - j});
        ans = min(ans, 1 + (mn + K - 1) / K);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
