#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const long long inf = 1LL << 60;
const int maxn = (int)1e3 + 5;
const long double eps = 1e-9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int res, n, m, k, f[maxn][maxn];
string a[maxn];
queue<pair<long long, int> > Q;
int dist(int x, int y) {
  int d = min(x, n - 1 - x);
  d = min(d, min(y, m - 1 - y));
  return d;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset(f, -1, sizeof f);
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'S') {
        Q.push(pair<long long, int>(i, j));
        f[i][j] = 1;
        if (dist(i, j) == 0) return cout << "0\n", 0;
      }
    }
  }
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int u = x + dx[i], v = y + dy[i];
      if (u < 0 || v < 0 || u >= n || v >= m || f[u][v] != -1 || a[u][v] == '#')
        continue;
      f[u][v] = f[x][y] + 1;
      Q.push(pair<long long, int>(u, v));
    }
  }
  res = mod;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (f[i][j] == -1 || f[i][j] > k) continue;
      res = min(res, (dist(i, j) + k - 1) / k);
    }
  }
  cout << res + 1 << endl;
}
