#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
long long h, w, k, a[1000][1000], si, sj;
string s;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  for (long long i = (1); i <= (h); i++) {
    string buf;
    cin >> buf;
    for (long long j = (1); j <= (w); j++) {
      a[i][j] = (buf[j - 1] == '#') ? -1 : h * w;
      if (buf[j - 1] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  queue<pair<long long, long long> > q;
  q.push(pair<long long, long long>(si, sj));
  a[si][sj] = 0;
  while (!q.empty()) {
    long long ai = q.front().first;
    long long aj = q.front().second;
    q.pop();
    if (a[si][sj] + 1 < a[si - 1][sj] && a[si][sj] + 1 <= k) {
      a[si - 1][sj] = a[si][sj] + 1;
      q.push(pair<long long, long long>(si - 1, sj));
    }
    if (a[si][sj] + 1 < a[si + 1][sj] && a[si][sj] + 1 <= k) {
      a[si + 1][sj] = a[si][sj] + 1;
      q.push(pair<long long, long long>(si + 1, sj));
    }
    if (a[si][sj] + 1 < a[si][sj - 1] && a[si][sj] + 1 <= k) {
      a[si][sj - 1] = a[si][sj] + 1;
      q.push(pair<long long, long long>(si, sj - 1));
    }
    if (a[si][sj] + 1 < a[si][sj + 1] && a[si][sj] + 1 <= k) {
      a[si][sj + 1] = a[si][sj] + 1;
      q.push(pair<long long, long long>(si, sj + 1));
    }
  }
  long long ans = h * w;
  for (long long i = (1); i <= (h); i++) {
    for (long long j = (1); j <= (w); j++) {
      if (a[i][j] != -1 && a[i][j] != h * w) {
        long long ansbuf =
            1 + min(min((i - 1 + k - 1) / k, (h - i + k - 1) / k),
                    min((j - 1 + k - 1) / k, (w - j + k - 1) / k));
        ans = min(ansbuf, ans);
      }
    }
  }
  cout << (ans) << endl;
  ;
  return 0;
}
