#include <bits/stdc++.h>
using namespace std;
char field[824][824];
int dist[824][824];
int h, w, k;
int res = 1000;
int xs[] = {0, 1, 0, -1};
int ys[] = {1, 0, -1, 0};
pair<int, int> start;
int calc(int i, int j, int h, int w) {
  int ans = abs(h - i);
  ans = min(ans, i);
  ans = min(ans, j);
  ans = min(ans, abs(w - j));
  return ans;
}
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> field[i][j];
      if (field[i][j] == 'S') {
        start = pair<int, int>(i, j);
      }
      dist[i][j] = -1;
    }
  }
  queue<pair<int, int> > q;
  q.push(start);
  dist[start.first][start.second] = 0;
  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    q.pop();
    int x = tmp.first, y = tmp.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + xs[i];
      int ny = y + ys[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
        continue;
      }
      if (field[nx][ny] == '#') continue;
      if (dist[nx][ny] != -1) continue;
      dist[nx][ny] = dist[x][y] + 1;
      q.push(pair<int, int>(nx, ny));
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (dist[i][j] == -1) continue;
      if (dist[i][j] > k) continue;
      int score = calc(i, j, h, w);
      int tmp = score / k + (score % k != 0) + 1;
      res = min(res, tmp);
    }
  }
  cout << res << endl;
  return 0;
}
