#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int h, w, k;
int sx, sy;
int maze[801][801];
bool used[801][801];
int ans = 1e9;
void check(int x, int y) {
  ans = min(ans, min(x, min(w - x - 1, min(y, h - y - 1))));
  return;
}
void bfs(int x, int y, int d) {
  if (d > k) return;
  used[y][x] = true;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (!used[ny][nx] && nx >= 0 && nx < w && ny >= 0 && ny < h &&
        maze[ny][nx] != '#') {
      bfs(nx, ny, d + 1);
      check(nx, ny);
    }
  }
  return;
}
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < s.size(); j++) {
      maze[i][j] = s[j];
      if (s[j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  bfs(sx, sy, 0);
  int ans2 = 1e9;
  ans2 = min(ans2, min(sx, min(w - sx - 1, min(sy, h - sy - 1))));
  ans = (ans % k == 0) ? ans / k : ans / k + 1;
  ans2 = (ans2 % k == 0) ? ans2 / k : ans2 / k + 1;
  cout << min(ans + 1, ans2 + 1) << endl;
}
