#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int> > mp(h, vector<int>(w, 0));
  vector<vector<int> > dd(h, vector<int>(w, 0));
  char c;
  pair<int, int> p;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c;
      if (c == '#') {
        mp[i][j] = 1;
      } else if (c == 'S') {
        mp[i][j] = 0;
        p.first = i;
        p.second = j;
      } else {
        mp[i][j] = 0;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dd[i][j] = abs(i - p.first) + abs(j - p.second);
    }
  }
  int dist =
      min(p.first, min(h - p.first - 1, min(p.second, w - p.second - 1)));
  int ans = 1000;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int aa = dd[i][j] / k;
      if (dd[i][j] % k != 0) {
        aa++;
      }
      if (mp[i][j] == 1 && aa == 1) {
        aa++;
      }
      if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
        ans = min(ans, aa);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
