#include <bits/stdc++.h>
using namespace std;
const int INF = 1000;
int H, W, K;
string S;
int sx, sy;
int near = INF;
int dist[801][801];
bool used[801][801];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int main() {
  cin >> H >> W >> K;
  cin.ignore();
  for (int i = 0; i < H; i++) fill(dist[i], dist[i] + W, INF);
  char maze[H][W];
  for (int i = 0; i < H; i++) {
    getline(cin, S);
    for (int j = 0; j < S.length(); j++) {
      maze[i][j] = S[j];
      if (maze[i][j] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }
  near = min(min(min(sx, W - sx - 1), sy), H - sy - 1);
  queue<pair<int, int> > Q;
  pair<int, int> q(sy, sx);
  Q.push(q);
  dist[sy][sx] = 0;
  while (!Q.empty()) {
    q = Q.front();
    used[q.first][q.second] = true;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = q.second + dx[i], ny = q.first + dy[i];
      if (!used[ny][nx] && nx >= 0 && ny >= 0 && nx < W && ny < H &&
          maze[ny][nx] != '#') {
        dist[ny][nx] = min(dist[ny][nx], dist[q.first][q.second] + 1);
        Q.push(pair<int, int>(ny, nx));
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (dist[i][j] <= K) {
        if (near > i) near = i;
        if (near > H - i - 1) near = H - i - 1;
        if (near > j) near = j;
        if (near > W - j - 1) near = W - j - 1;
      }
    }
  }
  if (near % K == 0)
    cout << near / K + 1 << endl;
  else
    cout << near / K + 2 << endl;
  return 0;
}
