#include <bits/stdc++.h>
using namespace std;
constexpr double PI = 3.1415926535897932;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < w; ++i) cin >> s[i];
  int sx = -1, sy = -1;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == 'S') {
        sx = i, sy = j;
        break;
      }
    }
    if (sx != -1) break;
  }
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[sx][sy] = 0;
  using T = tuple<int, int, int>;
  queue<T> que;
  que.push(T(0, sx, sy));
  while (!que.empty()) {
    T tup = que.front();
    que.pop();
    int d, x, y;
    tie(d, x, y) = tup;
    if (d > dist[x][y]) continue;
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
      if (s[nx][ny] == '#') continue;
      if (dist[nx][ny] >= dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        que.push(T(dist[nx][ny], nx, ny));
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (dist[i][j] > k) continue;
      int cost = 1;
      int d = min({i, h - i - 1, j, w - j - 1});
      cost += d / k;
      if (d % k != 0) ++cost;
      ans = min(ans, cost);
    }
  }
  cout << ans << endl;
}
