#include <bits/stdc++.h>
using namespace std;
using namespace std;
const long long inf = 1e9 + 7;
const long long mod = 998244353;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int main() {
  long long h, w, k;
  cin >> h >> w >> k;
  vector<vector<bool> > a(h, vector<bool>(w));
  long long sx, sy;
  for (int i = (0); i < (h); i++) {
    for (int j = (0); j < (w); j++) {
      char c;
      cin >> c;
      if (c == '#')
        ;
      else if (c == 'S') {
        a[i][j] = 1;
        sx = i;
        sy = j;
      } else
        a[i][j] = 1;
    }
  }
  vector<vector<int> > depth(h, vector<int>(w, inf));
  depth[sx][sy] = 0;
  vector<vector<bool> > visited(h, vector<bool>(w));
  queue<pair<int, int> > q;
  q.push(make_pair(sx, sy));
  while (!q.empty()) {
    pair<int, int> n = q.front();
    q.pop();
    int x = n.first, y = n.second;
    visited[x][y] = 1;
    for (int i = (0); i < (4); i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w) {
        if (visited[nx][ny] == 0 && a[nx][ny]) {
          q.push(make_pair(nx, ny));
          depth[nx][ny] = depth[x][y] + 1;
        }
      }
    }
  }
  long long mi = max(h, w);
  for (int i = (0); i < (h); i++) {
    for (int j = (0); j < (w); j++) {
      if (depth[i][j] <= k) {
        mi = min(mi, i * 1LL);
        mi = min(mi, (h - 1 - i) * 1LL);
        mi = min(mi, j * 1LL);
        mi = min(mi, (w - 1 - j) * 1LL);
      }
    }
  }
  long long ans = 1;
  if (mi == 0)
    ;
  else
    ans += (mi - 1) / k + 1;
  cout << ans << endl;
}
