#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};
int K;
int calc(int x) { return (x + K - 1) / K; }
int main() {
  ios::sync_with_stdio(false);
  int H, W;
  cin >> H >> W >> K;
  vector<string> S(H);
  for (int i = 0; i < (int)(H); i++) cin >> S[i];
  int sx, sy;
  for (int y = 0; y < (int)(H); y++) {
    for (int x = 0; x < (int)(W); x++) {
      if (S[y][x] == 'S') {
        S[y][x] = '.';
        sx = x, sy = y;
      }
    }
  }
  queue<pair<pair<int, int>, int>> Q;
  vector<vector<bool>> vis(H, vector<bool>(W));
  vis[sy][sx] = true;
  Q.emplace(pair<int, int>(sx, sy), 0);
  set<pair<int, int>> visitable;
  while (Q.size()) {
    pair<pair<int, int>, int> p = Q.front();
    Q.pop();
    if (p.second <= K) visitable.insert(p.first);
    if (p.second < K) {
      for (int i = 0; i < (int)(4); i++) {
        int nx = p.first.first + dx[i], ny = p.first.second + dy[i];
        if ((0 <= (int)(nx) && (int)(nx) < (int)(W) && 0 <= (int)(ny) &&
             (int)(ny) < (int)(H)) &&
            S[ny][nx] == '.') {
          Q.emplace(pair<int, int>(nx, ny), p.second + 1);
          vis[nx][ny] = true;
        }
      }
    }
  }
  int ans = INF;
  for (pair<int, int> p : visitable) {
    int tmp = min({calc(p.first), calc(p.second), calc(W - p.first - 1),
                   calc(H - p.second - 1)});
    ans = min(ans, tmp);
  }
  cout << ans + 1 << endl;
}
