#include <bits/stdc++.h>
using namespace std;
void solve(long long H, long long W, long long K, std::vector<std::string> A) {
  map<int, map<int, int>> dist;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i][j] = -1;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        dist[i][j] = 0;
        queue<pair<int, int>> que;
        que.push(make_pair(i, j));
        while (que.size()) {
          auto f = que.front();
          que.pop();
          for (int dx = -1; dx <= 1; dx++) {
            for (int dy = -1; dy <= 1; dy++) {
              if (dx == 0 && dy == 0) {
                continue;
              }
              if (dist[f.first + dx][f.second + dy] == -1 &&
                  A[f.first + dx][f.second + dy] != '#') {
                dist[f.first + dx][f.second + dy] = dist[f.first][f.second] + 1;
                que.push(make_pair(f.first + dx, f.second + dy));
              }
            }
          }
        }
      }
    }
  }
  int ans = numeric_limits<int>::max();
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (dist[i][j] != -1) {
        ans = min(ans,
                  (min({i, j, (int)H - 1 - i, (int)W - 1 - j}) + (int)K - 1) /
                      (int)K);
      }
    }
  }
  cout << ans + 1 << endl;
}
int main() {
  long long H;
  scanf("%lld", &H);
  long long W;
  scanf("%lld", &W);
  long long K;
  scanf("%lld", &K);
  std::vector<std::string> A(W);
  for (int i = 0; i < W; i++) {
    std::cin >> A[i];
  }
  solve(H, W, K, std::move(A));
  return 0;
}
