#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> Z(H);
  for (long long i = 0; i < (H); i++) cin >> Z[i];
  int S, T;
  for (long long i = 0; i < (H); i++)
    for (long long j = 0; j < (W); j++)
      if (Z[i][j] == 'S') {
        S = i;
        T = j;
      }
  vector<vector<int> > X(H, vector<int>(W, -1));
  X[S][T] = 0;
  queue<pair<int, int> > Q;
  Q.push(make_pair(S, T));
  vector<pair<int, int> > Y;
  Y.push_back(make_pair(S, T));
  while (!Q.empty()) {
    pair<int, int> tmp = Q.front();
    Q.pop();
    int A = tmp.first;
    int B = tmp.second;
    if (X[A][B] == K) break;
    if (A) {
      if (X[A - 1][B] == -1 and Z[A - 1][B] == '.') {
        X[A - 1][B] = X[A][B] + 1;
        Q.push(make_pair(A - 1, B));
        Y.push_back(make_pair(A - 1, B));
      }
    }
    if (A < H - 1) {
      if (X[A + 1][B] == -1 and Z[A + 1][B] == '.') {
        X[A + 1][B] = X[A][B] + 1;
        Q.push(make_pair(A + 1, B));
        Y.push_back(make_pair(A + 1, B));
      }
    }
    if (B) {
      if (X[A][B - 1] == -1 and Z[A][B - 1] == '.') {
        X[A][B - 1] = X[A][B] + 1;
        Q.push(make_pair(A, B - 1));
        Y.push_back(make_pair(A, B - 1));
      }
    }
    if (B < W - 1) {
      if (X[A][B + 1] == -1 and Z[A][B + 1] == '.') {
        X[A][B + 1] = X[A][B] + 1;
        Q.push(make_pair(A, B + 1));
        Y.push_back(make_pair(A, B + 1));
      }
    }
  }
  int ans = 1001001001;
  for (long long i = 0; i < (Y.size()); i++) {
    ans = min(
        ans,
        1 + (min({Y[i].first, H - Y[i].first, Y[i].second, W - Y[i].second}) +
             K - 1) /
                K);
  }
  cout << ans << endl;
}
