#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = 1e18;
constexpr long long MOD = 1e9 + 7;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};
int div_up(int a, int b) { return (a + b - 1) / b; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k, fh, fw;
  string a[810];
  cin >> h >> w >> k;
  for (int i = (0); i < (h); i++) cin >> a[i];
  for (int i = (0); i < (h); i++)
    for (int j = (0); j < (w); j++)
      if (a[i][j] == 'S') fh = i, fw = j;
  vector<pair<int, int> > v;
  int dis[810][810];
  memset(dis, -1, sizeof(dis));
  dis[fh][fw] = 0;
  queue<pair<int, int> > q;
  q.push(make_pair(fh, fw));
  while (q.size()) {
    pair<int, int> p = q.front();
    q.pop();
    bool add = false;
    for (int i = (0); i < (4); i++) {
      pair<int, int> p_ = p;
      p_.first += dy[i];
      p_.second += dx[i];
      if (p_.first < 0 || h <= p_.first || p_.second < 0 || w <= p_.second)
        continue;
      if (dis[p_.first][p_.second] != -1) continue;
      if (a[p_.first][p_.second] == '#') {
        if (!add) {
          v.push_back(p);
          add = true;
        }
        continue;
      }
      int d = dis[p.first][p.second] + 1;
      dis[p_.first][p_.second] = d;
      if (d <= k) q.push(p_);
    }
  }
  int res = INT_MAX;
  for (pair<int, int> p : v) {
    res = min(res, div_up(p.first, k));
    res = min(res, div_up(h - p.first - 1, k));
    res = min(res, div_up(p.second, k));
    res = min(res, div_up(w - p.second - 1, k));
  }
  cout << res + 1 << endl;
  return 0;
}
