#include <bits/stdc++.h>
using namespace std;
int h, w, d[810][810], k, sx, sy, ans = 1e9;
queue<pair<int, int> > p;
string s[800];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    cin >> s[1];
    for (int j = 0; j < w; j++) {
      d[i][j] = -1;
      if (s[i][j] == 'S') sx = i, sy = j;
    }
  }
  p.push(make_pair(sx, sy));
  d[sx][sy] = k;
  while (!p.empty()) {
    int x = p.front().first, y = p.front().second;
    p.pop();
    ans = min(ans, min(min(x - 1, y - 1), min(h - x, w - y)));
    if (x == 1 || y == 1 || x == h || y == w)
      ans = 0;
    else
      for (int r = 0; r < 4; r++) {
        int tx = x + dx[r], ty = y + dy[r];
        if (s[tx][ty] != '#' && d[tx][ty] < d[x][y] - 1) {
          d[tx][ty] = s[x][y] - 1;
          p.push(make_pair(tx, ty));
        }
      }
  }
  cout << (ans / k) + 1 << endl;
  return 0;
}
