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