#include <bits/stdc++.h>
using namespace std;
const int DY[] = {-1, 0, 1, 0};
const int DX[] = {0, 1, 0, -1};
int vis[801][801];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  int sy = -1, sx = -1;
  for (int(y) = (0); (y) < (int)(H); ++(y)) {
    cin >> A[y];
    if (sy == -1)
      for (int(x) = (0); (x) < (int)(W); ++(x))
        if (A[y][x] == 'S') {
          sy = y;
          sx = y;
          break;
        }
  }
  queue<tuple<int, int, int> > q;
  q.push({sy, sx, 0});
  while ((int)(q).size()) {
    int y, x, d;
    tie(y, x, d) = q.front();
    q.pop();
    vis[y][x]++;
    if (d < K)
      for (int(i) = (0); (i) < (int)(4); ++(i)) {
        int ny = y + DY[i], nx = x + DX[i];
        if (ny >= 0 && nx >= 0 && ny < H && nx < W && !vis[ny][nx] &&
            A[ny][nx] != '#') {
          q.push({ny, nx, d + 1});
        }
      }
  }
  int ans = INT_MAX;
  for (int(y) = (0); (y) < (int)(H); ++(y))
    for (int(x) = (0); (x) < (int)(W); ++(x)) {
      if (vis[y][x]) {
        (ans) = min((ans), (y));
        (ans) = min((ans), (x));
        (ans) = min((ans), (H - 1 - y));
        (ans) = min((ans), (W - 1 - x));
      }
    }
  ans = (ans + K - 1) / K + 1;
  cout << ans << endl;
}
