#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
int M[maxn][maxn], G[maxn][maxn];
queue<pair<int, int> > Q;
const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
int n, m, k, sn, sm;
char ch;
int abs(int x) { return x < 0 ? -x : x; }
int getans(int y, int x) {
  int d = min(y, n - y - 1);
  d = min(d, x);
  d = min(d, n - x - 1);
  if (d == 0) return 0;
  return (d - 1) / k + 1;
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> ch;
      if (ch == '#')
        G[i][j] = 1;
      else if (ch == 'S') {
        sn = i;
        sm = j;
      }
    }
  }
  M[sn][sm] = 1;
  Q.push(make_pair(sn, sm));
  while (!Q.empty()) {
    pair<int, int> s = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int ty = s.first + dy[i], tx = s.second + dx[i];
      if (ty < 0 || ty >= n) continue;
      if (tx < 0 || tx >= m) continue;
      if (G[ty][tx]) continue;
      if (abs(ty - sn) + abs(tx - sm) > k) continue;
      if (!M[ty][tx]) {
        M[ty][tx] = 1;
        Q.push(make_pair(ty, tx));
      }
    }
  }
  int ans = 1e9;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (M[i][j]) {
        ans = min(ans, getans(i, j));
      }
    }
  cout << ans + 1 << endl;
}
