#include <bits/stdc++.h>
using namespace std;
int main() {
  long long H, W, K, dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
  string S[800] = {};
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) cin >> S[i];
  queue<pair<pair<long long, long long>, long long> > Q;
  long long SX, SY;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (S[i][j] == 'S') SX = i, SY = j;
  Q.push({{SX, SY}, K});
  while (!Q.empty()) {
    pair<pair<long long, long long>, long long> PP = Q.front();
    Q.pop();
    pair<long long, long long> P = PP.first;
    long long X = PP.second;
    S[P.first][P.second] = 'X';
    if (P.first == 0 || P.first == H - 1 || P.second == 0 ||
        P.second == W - 1) {
      cout << 1 << endl;
      return 0;
    }
    if (X == 0) continue;
    for (int i = 0; i < 4; i++) {
      long long NX = P.first + dx[i], NY = P.second + dy[i];
      if (NX < 0 || H <= NX || NY < 0 || W <= NY) {
      }
      if (S[NX][NY] == '.') Q.push({{NX, NY}, X - 1});
    }
  }
  long long Dist = 1000;
  for (long long i = 0; i < H; i++) {
    for (long long j = 0; j < W; j++) {
      if (S[i][j] == 'X') {
        Dist = min({Dist, i, H - i - 1, j, W - j - 1});
      }
    }
  }
  cout << (Dist + K - 1) / K + 1 << endl;
}
