#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
string s[5000];
long long re[5000][5000];
long long cx[4] = {0, -1, 0, 1};
long long cy[4] = {1, 0, -1, 0};
bool ok(long long a, long long b) {
  return (0 <= a && a < n && 0 <= b && b < m);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  for (long long i = 0; i < n; i++) cin >> s[i];
  long long dx, dy;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++)
      if (s[i][j] == 'S') dx = i, dy = j;
  vector<pair<long long, long long> > c = {{dx, dy}};
  re[dx][dy] = 1;
  for (long long i = 0; i < c.size(); i++) {
    for (long long j = 0; j < 4; j++) {
      long long nx = c[i].first + cx[j], ny = c[i].second + cy[j];
      if (ok(nx, ny) && re[nx][ny] == 0 && s[nx][ny] != '#') {
        re[nx][ny] = re[c[i].first][c[i].second] + 1;
        if (re[nx][ny] <= k) {
          c.push_back({nx, ny});
          if (nx == 0 || nx == n - 1 || ny == 0 || ny == m - 1) {
            cout << 1;
            return 0;
          }
        }
      }
    }
  }
  long long ans = 4005;
  for (long long i = 0; i < c.size(); i++) {
    long long len = min(min(c[i].first, c[i].second),
                        min(n - 1 - c[i].first, m - 1 - c[i].second));
    ans = min(ans, (len + k - 1) / k);
  }
  cout << ans + 1;
}
