#include <bits/stdc++.h>
using namespace std;
bool inside(int x, int y, int xlim, int ylim) {
  return 0 <= x && x < xlim && 0 <= y && y < ylim;
}
int main() {
  constexpr int dd[] = {0, 1, 0, -1, 0};
  int h, w, k;
  cin >> h >> w >> k;
  queue<pair<int, int>> q;
  vector<string> s(h, string(w, ' '));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
      if (s[i][j] == 'S') {
        s[i][j] = '.';
        q.push(make_pair(i, j));
      }
    }
  vector<vector<bool>> done(h, vector<bool>(w, false));
  int ans = 1e9;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    if (done[p.first][p.second]) continue;
    done[p.first][p.second] = true;
    int dist = min({p.first, p.second, h - p.first, w - p.second});
    ans = min(ans, (dist + k - 1) / k);
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dd[i], ny = p.second + dd[i + 1];
      if (inside(nx, ny, h, w) && s[nx][ny] == '.') q.push(make_pair(nx, ny));
    }
  }
  cout << ans + 1 << endl;
}
