#include <bits/stdc++.h>
using namespace std;
int ans, h, w, f, g, k;
bool canvis[810][810], vis[810][810];
queue<pair<int, int> > Q;
queue<int> lenQ;
void BFS(int y, int x, int len) {
  vis[y][x] = true;
  int pnt = min(x, y);
  pnt = min(pnt, h - y - 1);
  pnt = min(pnt, w - x - 1);
  ans = min(ans, pnt);
  if (len == k) return;
  int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  for (int i = 0; i < 4; i++) {
    f = y + dy[i], g = x + dx[i];
    if (f >= 0 && g >= 0 && f < h && g < w) {
      if (canvis[f][g] && !vis[f][g]) {
        Q.push(make_pair(f, g));
        lenQ.push(len + 1);
      }
    }
  }
}
int main() {
  cin >> h >> w >> k;
  ans = 1145141919;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char z;
      cin >> z;
      canvis[i][j] = true;
      if (z == '#') canvis[i][j] = false;
      if (z == 'S') f = i, g = j;
    }
  }
  Q.push(make_pair(f, g));
  lenQ.push(0);
  while (!Q.empty()) {
    BFS(Q.front().first, Q.front().second, lenQ.front());
    Q.pop(), lenQ.pop();
  }
  int outs = ans / k + 1;
  if (ans % k > 0) outs++;
  cout << outs << endl;
  return 0;
}
