#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
signed main() {
  long long h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (long long i = 0; i < h; i++) cin >> a[i];
  queue<long long> qx;
  queue<long long> qy;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        qx.push(j);
        qy.push(i);
      }
    }
  }
  long long px[] = {1, 0, -1, 0};
  long long py[] = {0, -1, 0, 1};
  long long ans = 1e9;
  vector<vector<long long> > used(h, vector<long long>(w, 1));
  while (!qx.empty()) {
    long long x = qx.front();
    long long y = qy.front();
    qx.pop();
    qy.pop();
    ans = min(ans, min(x, min(y, min(w - x, h - y))));
    for (long long i = 0; i < 4; i++) {
      long long nx = x + px[i];
      long long ny = y + py[i];
      if (nx >= 0 && ny >= 0 && nx < w && ny < h && a[ny][nx] == '.' &&
          used[ny][nx] == 1) {
        used[ny][nx] = 0;
        qx.push(nx);
        qy.push(ny);
      }
    }
  }
  long long tmp = 1;
  if (ans % k != 0) tmp++;
  tmp += ans / k;
  cout << tmp << endl;
}
