#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;
vector<vector<int>> gridBFS(vector<string>& s, int sy, int sx,
                            char wall = '#') {
  const int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
  int h = s.size(), w = s[0].size();
  vector<vector<int>> dist(h, vector<int>(w, -1));
  queue<pair<int, int>> que;
  que.emplace(sy, sx);
  dist[sy][sx] = 0;
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i], nx = p.second + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] != wall &&
          dist[ny][nx] == -1) {
        que.emplace(ny, nx);
        dist[ny][nx] = dist[p.first][p.second] + 1;
      }
    }
  }
  return dist;
}
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (int i = 0; i < (h); i++) {
    cin >> a[i];
  }
  int sy, sx;
  for (int i = 0; i < (h); i++) {
    for (int j = 0; j < (w); j++) {
      if (a[i][j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  vector<vector<int>> dist = gridBFS(a, sy, sx);
  int ans = INF;
  for (int i = 0; i < (h); i++) {
    for (int j = 0; j < (w); j++) {
      if (dist[i][j] <= k) {
        ans = min(ans, 1 + min({(i + k - 1) / k, ((h - 1) - i + k - 1) / k,
                                (j + k - 1) / k, ((w - 1) - j + k - 1) / k}));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
