#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int H, W, K;
string A[810];
int main() {
  cin >> H >> W >> K;
  int startX, startY;
  for (int i = 0; i < H; i++) {
    cin >> A[i];
    for (int j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        startY = i;
        startX = j;
      }
    }
  }
  int d[810][810];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) d[i][j] = INF;
  queue<pair<int, int> > tq;
  queue<pair<int, int> > q;
  tq.push(make_pair(startY, startX));
  q.push(make_pair(startY, startX));
  d[startY][startX] = 0;
  while (!tq.empty()) {
    pair<int, int> nowPos = tq.front();
    tq.pop();
    for (int k = 0; k < 4; k++) {
      int nextY = nowPos.first + dy[k], nextX = nowPos.second + dx[k];
      if (!(0 <= nextY && nextY < H) || !(0 <= nextX && nextX < W)) continue;
      if (A[nextY][nextX] == '#' || d[nextY][nextX] != INF) continue;
      d[nextY][nextX] = 0;
      tq.push(make_pair(nextY, nextX));
      q.push(make_pair(nextY, nextX));
    }
  }
  int ans = INF;
  while (!q.empty()) {
    pair<int, int> nowPos = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      int nextY = nowPos.first + dy[k], nextX = nowPos.second + dx[k];
      if (!(0 <= nextY && nextY < H) || !(0 <= nextX && nextX < W)) continue;
      if (d[nextY][nextX] != INF) continue;
      d[nextY][nextX] = d[nowPos.first][nowPos.second] + 1;
      q.push(make_pair(nextY, nextX));
      if (nextY == 0 || nextY == H - 1 || nextX == 0 || nextX == W - 1) {
        ans = min(ans, d[nextY][nextX]);
      }
    }
  }
  cout << (ans + (K - 1)) / K + 1 << endl;
  return 0;
}
