#include <bits/stdc++.h>
using ll = long long;
using namespace std;
char g[900][900];
int vis[900][900];
int H, W, K;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> H >> W >> K;
  int x0, y0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> g[i][j];
      if (g[i][j] == 'S') {
        x0 = i;
        y0 = j;
      }
    }
  }
  vis[x0][y0] = 1;
  vector<pair<int, int>> pos;
  pos.push_back({x0, y0});
  auto checkAdd = [](const int x, const int y, vector<pair<int, int>> &npos) {
    if (x < 0 || x >= H || y < 0 || y >= W) return;
    if (vis[x][y] || g[x][y] == '#') return;
    vis[x][y] = 1;
    npos.push_back({x, y});
  };
  int ct = 0;
  vector<pair<int, int>> apos;
  while (ct < K && pos.size()) {
    vector<pair<int, int>> npos;
    for (auto &p : pos) {
      apos.push_back(p);
      int x = p.first;
      int y = p.second;
      checkAdd(x + 1, y, npos);
      checkAdd(x - 1, y, npos);
      checkAdd(x, y + 1, npos);
      checkAdd(x, y - 1, npos);
    }
    swap(pos, npos);
    ct++;
  }
  int ret = 800 * 800 + 1;
  for (auto &p : apos) {
    int x = p.first;
    int y = p.second;
    int mstep = min({x, y, H - x - 1, W - y - 1});
    ret = min(ret, (mstep + K - 1) / K);
  }
  cout << ret + 1 << endl;
}
