#include <bits/stdc++.h>
using namespace std;
const int INF = 1000;
int H, W, K;
string S;
char maze[801][801];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int dist[801][801];
int ans = INF;
int sx, sy;
void nearest(int y, int x) {
  int near[4] = {y, x, H - y - 1, W - x - 1};
  for (int i = 0; i < 4; i++) {
    ans = min(ans, near[i]);
  }
}
int main() {
  cin >> H >> W >> K;
  cin.ignore();
  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;
      }
    }
  }
  for (int i = 0; i < H; i++) fill(dist[i], dist[i] + W, INF);
  queue<pair<int, int> > Q;
  pair<int, int> q(sy, sx);
  Q.push(q);
  dist[sy][sx] = 0;
  nearest(sy, sx);
  while (!Q.empty()) {
    q = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = q.second + dx[i], ny = q.first + dy[i];
      if (dist[ny][nx] == INF && nx >= 0 && nx < W && ny >= 0 && ny < H &&
          maze[ny][nx] != '#') {
        dist[ny][nx] = dist[q.first][q.second] + 1;
        if (dist[ny][nx] <= K) nearest(ny, nx);
      }
    }
  }
  cout << ceil(ans / (double)K) + 1 << endl;
}
