#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-5;
const int INF = 1e9;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int>> m(w, vector<int>(h)), d(w, vector<int>(h));
  int sx, sy;
  for (int j = (0); j < (h); ++j) {
    string s;
    cin >> s;
    for (int i = (0); i < (w); ++i) {
      if (s[i] == '#')
        m[i][j] = 1;
      else if (s[i] == 'S') {
        sx = i;
        sy = j;
        m[i][j] = 0;
      } else
        m[i][j] = 0;
    }
  }
  function<void(int, int, int)> dfs = [&](int x, int y, int p) {
    if (d[x][y]) return;
    if (m[x][y] == 1) return;
    d[x][y] = 1;
    if (p == 0) return;
    for (int i = (0); i < (4); ++i) {
      int px = x + dx[i], py = y + dy[i];
      if (0 <= px && px < w && 0 <= py && py < h) dfs(px, py, p - 1);
    }
  };
  dfs(sx, sy, k);
  if (sx == 0 || sy == 0 || sx == w - 1 || sy == h - 1) {
    cout << 0 << endl;
    return -1;
  }
  int ans = INF;
  for (int j = (0); j < (h); ++j) {
    for (int i = (0); i < (w); ++i) {
      if (d[i][j]) {
        int nx = min(i, w - i - 1);
        int ny = min(j, h - j - 1);
        ans = min(ans, min(nx, ny));
      }
    }
  }
  int aans = (ans + k - 1) / k;
  cout << aans + 1 << endl;
}
