#include <bits/stdc++.h>
using namespace std;
const long long INF = INT_MAX;
const long long MOD = 998244353;
int di[4] = {0, 1, 0, -1};
int dj[4] = {1, 0, -1, 0};
struct position {
  long long i, j, depth;
};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (long long i = 0; (i) < (H); ++(i)) cin >> A[i];
  queue<position> Q;
  for (long long i = 0; (i) < (H); ++(i)) {
    for (long long j = 0; (j) < (W); ++(j)) {
      if (A[i][j] == 'S') {
        Q.push({i, j, 0});
        break;
      }
    }
  }
  int rest = INF;
  vector<vector<bool>> visit(H, vector<bool>(W, false));
  while (!Q.empty()) {
    position u = Q.front();
    Q.pop();
    if (visit[u.i][u.j]) continue;
    visit[u.i][u.j] = true;
    rest = min(min(u.i, H - 1 - u.i), min(u.j, W - 1 - u.j));
    for (long long d = 0; (d) < (4); ++(d)) {
      long long ni = u.i + di[d];
      long long nj = u.j + dj[d];
      if (ni < 0 || H <= ni || nj < 0 || W <= nj || A[ni][nj] == '#') continue;
      Q.push({ni, nj, u.depth + 1});
    }
  }
  int ans = (rest ? (rest - 1) / K + 1 : 0) + 1;
  cout << ans << endl;
  return 0;
}
