#include <bits/stdc++.h>
using namespace std;
class CClosedRooms {
 public:
  static constexpr long long kStressIterations = 0;
  static void generateTest(std::ostream& test) {}
  long long dx[4] = {-1, 0, 0, 1};
  long long dy[4] = {0, -1, 1, 0};
  long long H, W, K;
  vector<pair<long long, long long>> v;
  set<pair<long long, long long>> used;
  vector<vector<char>> A;
  void dfs(pair<long long, long long> p, long long n) {
    v.push_back(p);
    if (n == K) {
      return;
    }
    long long x, y;
    tie(x, y) = p;
    used.insert(p);
    for (long long i = 0, i_len = (4); i < i_len; ++i) {
      long long nx = x + dx[i];
      long long ny = y + dy[i];
      if (nx < 0 || nx > H - 1 || ny < 0 || ny > W - 1) continue;
      if (A[nx][ny] == '#') continue;
      if (used.count({nx, ny})) continue;
      dfs({nx, ny}, n + 1);
    }
  }
  void solve(std::istream& cin, std::ostream& cout) {
    cin >> H >> W >> K;
    A = vector<vector<char>>(H, vector<char>(W));
    long long sx, sy;
    for (long long i = 0, i_len = (H); i < i_len; ++i) {
      for (long long j = 0, j_len = (W); j < j_len; ++j) {
        cin >> A[i][j];
        if (A[i][j] == 'S') {
          sx = i, sy = j;
        }
      }
    }
    dfs({sx, sy}, 0);
    long long res = 1001001001001001001ll;
    for (long long i = 0, i_len = (v.size()); i < i_len; ++i) {
      long long x, y;
      tie(x, y) = v[i];
      long long tmp = min({x, H - 1 - x, y, W - 1, y});
      res = min(res, (tmp + K - 1) / K);
    }
    cout << res + 1 << endl;
  }
};
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  CClosedRooms solver;
  solver.solve(in, out);
  return 0;
}
