#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int h, w, k;
char r[900][900];
int dp[900][900];
int main() {
  cin >> h >> w >> k;
  int sa, sb;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> r[i][j];
      if (r[i][j] == 'S') {
        sa = i, sb = j;
      }
    }
  }
  queue<pair<pair<int, int>, int> > que;
  que.push(make_pair(make_pair(sa, sb), 0));
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  int ans = 900;
  while (que.size()) {
    auto f = que.front();
    que.pop();
    int x = f.first.second, y = f.first.first;
    double mx = min(w - 1 - x, x);
    double my = min(h - 1 - y, y);
    if (mx == 0 || my == 0)
      ans = 1;
    else
      ans = min(ans, 1 + int(ceil(min(mx, my) / k)));
    if (f.second + 1 <= k) {
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx >= 0 && nx < w && ny >= 0 && ny < h && r[ny][nx] == '.') {
          que.push(make_pair(make_pair(ny, nx), f.second + 1));
        }
      }
    }
  }
  cout << ans << endl;
}
