#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using P = pair<int, int>;
using P3 = pair<P, ll>;
using PP = pair<P, P>;
constexpr int INF = 1 << 30;
constexpr ll MOD = ll(1e9) + 7;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr double EPS = 1e-9;
int h, w, k;
vector<vector<int> > d;
bool isIn(int i, int j) { return i >= 0 && i < h && j >= 0 && j < w; }
int solve() {
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  vector<vector<bool> > visited(h, vector<bool>(w));
  queue<P3> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        visited[i][j] = true;
        que.push(P3(P(i, j), k - 1));
      }
    }
  }
  while (!que.empty()) {
    auto tmp = que.front();
    que.pop();
    int i = tmp.first.first, j = tmp.first.second, r = tmp.second;
    if (r == 0) continue;
    for (int l = 0; l < 4; l++) {
      int ni = i + di[l], nj = j + dj[l];
      if (isIn(ni, nj) && s[ni][nj] == '.' && !visited[ni][nj]) {
        visited[ni][nj] = true;
        que.push(P3(P(ni, nj), r - 1));
      }
    }
  }
  int res = INF;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (visited[i][j]) {
        res = min(res,
                  (min(min(i, h - 1 - i), min(j, w - 1 - j)) + k - 1) / k + 1);
      }
    }
  }
  return res;
}
int main() {
  cout << solve() << endl;
  return 0;
}
