#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int INF = 1e9;
int nokori(int i, int j, int h, int w) {
  return min({i, j, h - i - 1, w - j - 1});
}
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; ++i) cin >> s[i];
  int si, sj;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (s[i][j] == 'S') si = i, sj = j;
  vector<vector<int> > dist(h, vector<int>(w, INF));
  queue<pair<int, int> > que;
  que.push(pair<int, int>(si, sj));
  dist[si][sj] = 0;
  int di[] = {1, -1, 0, 0}, dj[] = {0, 0, 1, -1};
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    int i = p.first, j = p.second;
    for (int k = 0; k < 4; ++k) {
      int ii = i + di[k], jj = j + dj[k];
      if (ii < 0 || ii >= h || jj < 0 || jj >= w || s[ii][jj] == '#') continue;
      if (dist[ii][jj] != INF) continue;
      dist[ii][jj] = dist[i][j] + 1;
      if (dist[ii][jj] == k) continue;
      que.push(pair<int, int>(ii, jj));
    }
  }
  int ans = INF;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) {
      if (dist[i][j] == INF) continue;
      ans = min(ans, 1 + (nokori(i, j, h, w) + k - 1) / k);
    }
  cout << ans << endl;
  return 0;
}
