#include <bits/stdc++.h>
int main() {
  long long h, w, k, sx, sy;
  std::cin >> h >> w >> k;
  std::vector<std::string> v(h);
  for (long long i = 0; i < h; i++) {
    std::cin >> v[i];
    auto pos = std::find(v[i].begin(), v[i].end(), 'S');
    if (pos != v[i].end()) {
      sx = i;
      sy = pos - v[i].begin();
    }
  }
  std::vector<std::vector<long long> > cost(h,
                                            std::vector<long long>(w, 1000000));
  cost[sx][sy] = 0;
  std::stack<std::pair<long long, long long> > s;
  s.push({sx, sy});
  std::vector<std::string> up(h);
  std::string ss = v[0];
  for (int i = 0; i < w; i++) {
    ss[i] = '#';
  }
  for (int i = 0; i < h; i++) {
    up[i] = ss;
  }
  up[sx][sy] = '.';
  int xx[] = {1, 0, -1, 0}, yy[] = {0, 1, 0, -1};
  while (!s.empty()) {
    int x = s.top().first, y = s.top().second;
    s.pop();
    for (int i = 0; i < 4; i++) {
      if (x + xx[i] < 0 || x + xx[i] >= h || y + yy[i] < 0 || y + yy[i] >= w) {
        continue;
      }
      if (v[x + xx[i]][y + yy[i]] == '.' && up[x + xx[i]][y + yy[i]] == '#') {
        s.push({x + xx[i], y + yy[i]});
        up[x + xx[i]][y + yy[i]] = '.';
      }
    }
  }
  cost[sx][sy] = 1;
  long long ans = 10000000000;
  for (int j = 0; j < h; j++) {
    for (int l = 0; l < w; l++) {
      if (up[j][l] == '.') {
        ans = std::min(ans, static_cast<long long>(std::ceil(1.0 * j / k)));
        ans = std::min(
            ans, static_cast<long long>(std::ceil(1.0 * (h - j - 1) / k)));
        ans = std::min(ans, static_cast<long long>(std::ceil(1.0 * l / k)));
        ans = std::min(
            ans, static_cast<long long>(std::ceil(1.0 * (w - l - 1) / k)));
      }
    }
  }
  std::cout << ans + 1 << std::endl;
  return 0;
}
