#include <bits/stdc++.h>
using namespace std;
long long n, m, x;
string s[805];
long long sx, sy;
queue<long long> qx, qy, qi;
bool v[805][805];
long long ans = 4e18;
long long dx[4] = {0, 1, 0, -1};
long long dy[4] = {1, 0, -1, 0};
int main() {
  scanf("%lld%lld%lld", &n, &m, &x);
  for (long long i = 0; i < n; i++) {
    cin >> s[i];
    for (long long j = 0; j < m; j++) {
      if (s[i][j] == 'S') sx = i, sy = j;
    }
  }
  v[sx][sy] = true;
  qx.push(sx), qy.push(sy), qi.push(0);
  while (!qx.empty()) {
    long long t1 = qx.front();
    long long t2 = qy.front();
    long long t3 = qi.front();
    qx.pop(), qy.pop(), qi.pop();
    if (t3 > x) break;
    ans = min(ans, t1);
    ans = min(ans, t2);
    ans = min(ans, n - t1 - 1);
    ans = min(ans, m - t2 - 1);
    for (long long i = 0; i < 4; i++) {
      if (sx + dx[i] < 0 || sx + dx[i] >= n) continue;
      if (sy + dy[i] < 0 || sy + dy[i] >= n) continue;
      if (s[sx + dx[i]][sy + dy[i]] == '#') continue;
      if (v[sx + dx[i]][sy + dy[i]]) continue;
      v[sx + dx[i]][sy + dy[i]] = true;
      qx.push(sx + dx[i]), qy.push(sy + dy[i]), qi.push(t3 + 1);
    }
  }
  printf("%lld\n", (ans + 2 * x - 1) / x);
  return 0;
}
