#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        vector<pair<int, int>> que(1, make_pair(i, j));
        vector<int> dist(1, 0);
        for (int b = 0; b < (int)que.size(); b++) {
          if (dist[b] == k) {
            break;
          }
          for (int di = -1; di <= 1; di++) {
            for (int dj = -1; dj <= 1; dj++) {
              if (di * di + dj * dj != 1) {
                continue;
              }
              int ni = que[b].first + di;
              int nj = que[b].second + dj;
              if (ni >= 0 && nj >= 0 && ni < h && nj < w) {
                if (s[ni][nj] == '.') {
                  que.emplace_back(ni, nj);
                  s[ni][nj] = 'S';
                  dist.push_back(dist[b] + 1);
                }
              }
            }
          }
        }
      }
    }
  }
  int ans = (int)1e9;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        int d = min(i, min(j, min(h - 1 - i, w - 1 - j)));
        ans = min(ans, 1 + (d + k - 1) / k);
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
