#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sy = 0, sx = 0;
  vector<vector<char>> a(800, vector<char>(800));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a.at(i).at(j);
      if (a.at(i).at(j) == 'S') {
        sy = i;
        sx = j;
        a.at(i).at(j) = '.';
      }
    }
  }
  int dy[4] = {1, -1, 0, 0};
  int dx[4] = {0, 0, 1, -1};
  const int INF = 800 * 800;
  using P = pair<int, int>;
  queue<P> que;
  que.push(P(sy, sx));
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[sy][sx] = 0;
  while (!que.empty()) {
    int ny = que.front().first, nx = que.front().second;
    que.pop();
    if (dist.at(ny).at(nx) == k) continue;
    for (int i = 0; i < 4; i++) {
      int y = ny + dy[i], x = nx + dx[i];
      if ((0 <= y && y < h) && (0 <= x && x < w) && (dist.at(y).at(x) == INF) &&
          (a.at(y).at(x) == '.')) {
        que.push(P(y, x));
        dist.at(y).at(x) = dist.at(ny).at(nx) + 1;
      }
    }
  }
  int mini = 800 * 800;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (dist.at(i).at(j) > k) continue;
      if ((i == 0) || (i == h - 1) || (j == 0) || (j == w - 1))
        mini = 1;
      else
        mini = min(mini, (min({i, h - i, j, w - j}) + k - 1) / k + 1);
    }
  }
  cout << mini << endl;
}
