#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int M, N, K;
int bx, by;
int visited[801][801];
string rooms[801];
queue<int> q;
bool isInMap(int x, int y);
int main() {
  cin >> M >> N >> K;
  for (int i = 0; i < M; i++) {
    cin >> rooms[i];
    for (int j = 0; j < N; j++) {
      if (rooms[i][j] == 'S') {
        bx = i;
        by = j;
        rooms[i][j] == '.';
      }
      visited[i][j] = -1;
    }
  }
  q.push(bx);
  q.push(by);
  visited[bx][by] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if ((visited[nx][ny] <= visited[x][y] + 1 && visited[nx][ny] != -1) ||
          rooms[nx][ny] == '#') {
        continue;
      }
      visited[nx][ny] = visited[x][y] + 1;
      q.push(nx);
      q.push(ny);
    }
  }
  int res = 2147483647;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (visited[i][j] != -1 && visited[i][j] <= K) {
        res = min(res,
                  1 + (min(min(i, N - i - 1), min(j, M - j - 1)) + K - 1) / K);
      }
    }
  }
  cout << res << endl;
  return 0;
}
bool isInMap(int x, int y) { return x >= 0 && y >= 0 && x < N && y < M; }
