#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = uint32_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;
const ll mod = ((ll)1e9) + 7;
const ll INF = 1e9;
template <typename F>
double bsearch(double ok, double ng, ll loop, F f) {
  for (auto i = 0 * (loop), i_len = (loop); i < i_len; ++i) {
    double middle = (ok + ng) / 2;
    (f(middle) ? ok : ng) = middle;
  }
  return ok;
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(50);
  ll h, w, k;
  cin >> h >> w >> k;
  vvb maze(h, vb(w, false));
  ll sx, sy;
  for (auto i = 0 * (h), i_len = (h); i < i_len; ++i)
    for (auto j = 0 * (w), j_len = (w); j < j_len; ++j) {
      char c;
      cin >> c;
      if (c == '#') maze[i][j] = true;
      if (c == 'S') {
        sx = j;
        sy = i;
      }
    }
  vvl dict(h, vl(w, ((ll)1e10)));
  vl dx = {0, 1, 0, -1};
  vl dy = {1, 0, -1, 0};
  using P = tuple<ll, ll, ll>;
  deque<P> q;
  q.push_back(P{sx, sy, 0});
  ll mi = ((ll)1e10);
  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    ll x = get<0>(p);
    ll y = get<1>(p);
    ll d = get<2>(p);
    if (dict[y][x] < d) continue;
    mi = min(mi, x);
    mi = min(mi, w - x - 1);
    mi = min(mi, y);
    mi = min(mi, h - y - 1);
    for (auto i = 0 * (4), i_len = (4); i < i_len; ++i) {
      ll nx = dx[i] + x;
      ll ny = dy[i] + y;
      if (!maze[ny][nx]) {
        if (nx == 0 || ny == 0 || nx == w - 1 || ny == h - 1) {
          cout << 1 << endl;
          return 0;
        }
        if (dict[ny][nx] > d + 1) {
          dict[ny][nx] = d + 1;
          q.push_back(P{nx, ny, d + 1});
        }
      }
    }
  }
  cout << mi / k + (mi % k != 0) + 1 << endl;
  return 0;
}
