#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;
  string s[h];
  for (int i = 0; i < h; i++) cin >> s[i];
  queue<pair<pair<int, int>, int>> q;
  int ans = 1e9;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (s[i][j] == 'S') {
        s[i][j] = '.';
        q.push(make_pair(make_pair(i, j), 0));
      }
  int muni[h][w];
  fill(muni[0], muni[h], -1);
  while (!q.empty()) {
    auto puni = q.front();
    auto p = puni.first;
    q.pop();
    if ((muni[p.first][p.second] != -1 &&
         muni[p.first][p.second] <= puni.second) ||
        k < puni.second)
      continue;
    muni[p.first][p.second] = puni.second;
    int d = min({p.first, p.second, h - p.first, w - p.second});
    ans = min(ans, (d + 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(make_pair(nx, ny), puni.second + 1));
      }
    }
  }
  cout << ans + 1 << endl;
}
