#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  char a[801][801];
  int sx, sy;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  vector<vector<int> > tesu(h, vector<int>(w, -1));
  queue<pair<int, int> > qu;
  qu.push(make_pair(sx, sy));
  tesu[sx][sy] = 0;
  int ans = (min(min(sx - 0, h - 1 - sx), min(sy - 0, w - 1 - sy)) - 1) / k + 2;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  while (!qu.empty()) {
    int x = (qu.front()).first;
    int y = (qu.front()).second;
    qu.pop();
    if (tesu[x][y] >= k) continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (tesu[nx][ny] >= 0) continue;
      tesu[nx][ny] = tesu[x][y] + 1;
      if (nx == 0 || nx == h - 1 || ny == 0 || ny == w - 1)
        ans = 1;
      else
        ans = min(ans, (min(min(nx - 0, h - 1 - nx), min(ny - 0, w - 1 - ny)) -
                        1) / k +
                           2);
    }
  }
  cout << ans << endl;
  return 0;
}
