#include <bits/stdc++.h>
using namespace std;
using i32 = int;
using i64 = long long int;
using f64 = double;
using str = string;
template <typename T>
using vec = vector<T>;
template <typename T>
using heap = priority_queue<T, vec<T>, greater<T>>;
const i64 MOD = 1000000007;
const f64 EPS = 1e-10;
i32 h, w, k;
bool a[800][800];
i32 sx, sy;
i32 ox[4] = {-1, 0, 1, 0};
i32 oy[4] = {0, -1, 0, 1};
i32 solve(i32 fx, i32 fy, i32 tx, i32 ty) {
  bool fp[800][800] = {};
  set<tuple<i32, i32, i32>> s;
  vec<pair<i32, i32>> v;
  s.insert(make_tuple(0, fy, fx));
  while (s.size() > 0) {
    i32 d, y, x;
    tie(d, y, x) = *s.begin();
    s.erase(s.begin());
    if (fp[y][x]) {
      continue;
    }
    v.emplace_back(make_pair(y, x));
    fp[y][x] = true;
    if (d >= k) {
      continue;
    }
    for (i32 i = 0; i < (4); ++i) {
      i32 nx, ny;
      nx = x + ox[i];
      ny = y + oy[i];
      if (0 <= nx && nx < w && 0 <= ny && ny < h) {
        if (a[ny][nx] && !fp[ny][nx]) {
          s.insert(make_tuple(d + 1, ny, nx));
        }
      }
    }
  }
  i32 ans = INT_MAX;
  for (i32 i = 0; i < (v.size()); ++i) {
    i32 d = abs(v[i].second - ty) + abs(v[i].first - tx);
    if ((d % k) == 0) {
      ans = min(ans, d / k + 1);
    } else {
      ans = min(ans, d / k + 2);
    }
  }
  return ans;
}
i32 main() {
  cin >> h >> w >> k;
  for (i32 i = 0; i < (h); ++i) {
    str s;
    cin >> s;
    for (i32 j = 0; j < (w); ++j) {
      a[i][j] = s[j] != '#';
      if (s[j] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }
  i32 ans = INT_MAX;
  for (i32 i = (0); i < (w - 1); ++i) {
    ans = min(ans, solve(sx, sy, i, 0));
    ans = min(ans, solve(sx, sy, i, h - 1));
  }
  for (i32 i = (0); i < (h - 1); ++i) {
    ans = min(ans, solve(sx, sy, 0, i));
    ans = min(ans, solve(sx, sy, w - 1, i));
  }
  cout << ans << endl;
  return 0;
}
