#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979323846264338327950288;
char a[800][800];
const long long inf = 99999999999999;
int b[800][800];
struct Q {
  long long x, y;
};
int u1[4] = {0, 0, -1, 1}, u2[4] = {-1, 1, 0, 0};
signed main() {
  long long h, w, x, y, k;
  cin >> h >> w >> k;
  for (long long i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (long long j = 0; j < w; j++) {
      a[i][j] = s[j];
      b[i][j] = inf;
      if (s[j] == 'S') x = j, y = i;
    }
  }
  queue<Q> q;
  q.push(Q{x, y});
  b[y][x] = 0;
  long long ans = inf;
  while (!q.empty()) {
    Q t = q.front();
    q.pop();
    ans = ceil(min(ans, min(t.x, min(w - 1 - t.x, min(h - 1 - t.y, t.y)))) *
               1.0 / k) +
          1;
    for (int i = 0; i < 4; i++) {
      long long x = t.x + u1[i];
      long long y = t.y + u2[i];
      if (x < w && x >= 0 && y < h && w >= 0 && a[y][x] == '.' &&
          b[t.y][t.x] + 1 < b[y][x] && b[t.y][t.x] + 1 <= k) {
        q.push(Q{x, y});
        b[y][x] = b[t.y][t.x] + 1;
      }
    }
  }
  cout << ans << endl;
  cin >> h;
}
