#include <bits/stdc++.h>
using namespace std;
constexpr double EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  int sx, sy;
  vector<vector<int>> dis(h, vector<int>(w, INF));
  for (int i = 0; i < h; i++) {
    cin >> a[i];
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
        a[i][j] = '.';
      }
    }
  }
  queue<pair<int, int>> q;
  q.push(pair<int, int>(sy, sy));
  dis[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    if (dis[p.first][p.second] >= k) continue;
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny) continue;
      if (a[nx][ny] == '#') continue;
      if (dis[nx][ny] != INF) continue;
      q.push(pair<int, int>(nx, ny));
      dis[nx][ny] = dis[p.first][p.second] + 1;
    }
  }
  vector<vector<int>> res(h, vector<int>(w, INF));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (dis[i][j] == INF) continue;
      if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
        cout << 1 << endl;
        return 0;
      }
      q.push(pair<int, int>(i, j));
      res[i][j] = 0;
    }
  }
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny) continue;
      if (res[nx][ny] != INF) continue;
      q.push(pair<int, int>(nx, ny));
      res[nx][ny] = res[p.first][p.second] + 1;
    }
  }
  int ans = INF;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i != 0 && i != h - 1 && j != 0 && j != w - 1) continue;
      ans = min(ans, 1 + (res[i][j] - 1) / k + 1);
    }
  }
  cout << ans << endl;
}
