#include <bits/stdc++.h>
using namespace std;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int H, W, K;
char t[1000][1000];
int d[1000][1000];
int si, sj;
void bfs() {
  int ans = 1e9;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) d[i][j] = 1e9;
  queue<int> qy, qx;
  qy.push(si);
  qx.push(sj);
  d[si][sj] = 0;
  while (!qy.empty()) {
    int y = qy.front();
    qy.pop();
    int x = qx.front();
    qx.pop();
    int f = min(y, x);
    f = min(f, H - 1 - y);
    f = min(f, W - 1 - x);
    ans = min(ans, 1 + (d[y][x] + f) / K);
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || nx < 0 || ny >= H || nx >= W) continue;
      if (t[ny][nx] == '#') continue;
      int ncost = d[y][x] + 1;
      if (d[ny][nx] > ncost) {
        d[ny][nx] = ncost;
        qy.push(ny);
        qx.push(nx);
      }
    }
  }
  cout << ans << endl;
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> t[i][j];
      if (t[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  bfs();
  return 0;
}
