#include <bits/stdc++.h>
using namespace std;
long long h, w, k, cx, cy, u, d, l, r;
char map[876][876];
bool bm[876][876];
queue<pair<pair<long long, long long>, long long>> q;
signed main() {
  cin >> h >> w >> k;
  for (long long i = 1; i <= h; i++) {
    for (long long j = 1; j <= w; j++) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        cx = i;
        cy = j;
      }
    }
  }
  u = h;
  d = h;
  l = w;
  r = w;
  q.push(make_pair(make_pair(cx, cy), 0));
  while (q.size()) {
    long long x = q.front().first.first, y = q.front().first.second,
              p = q.front().second;
    bm[x][y] = 1;
    u = min(u, (x - 1 + k - 1) / k);
    d = min(d, (h - x + k - 1) / k);
    l = min(l, (y - 1 + k - 1) / k);
    r = min(r, (w - y + k - 1) / k);
    if (p < k) {
      if (map[x - 1][y] == '.' && !bm[x - 1][y])
        q.push(make_pair(make_pair(x - 1, y), p + 1));
      if (map[x + 1][y] == '.' && !bm[x + 1][y])
        q.push(make_pair(make_pair(x + 1, y), p + 1));
      if (map[x][y - 1] == '.' && !bm[x][y - 1])
        q.push(make_pair(make_pair(x, y - 1), p + 1));
      if (map[x][y + 1] == '.' && !bm[x][y + 1])
        q.push(make_pair(make_pair(x, y + 1), p + 1));
    }
    q.pop();
  }
  cout << min(min(u, d), min(l, r)) + 1 << endl;
}
