#include <bits/stdc++.h>
using namespace std;
const int N = 808;
char c[N][N];
pair<int, int> dp[N][N];
int ans = 1e9;
int h, w, K;
queue<tuple<int, int, int>> q;
set<pair<int, int>> s;
int main() {
  cin >> h >> w >> K;
  int starti, startj;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'S') {
        starti = i;
        startj = j;
      }
    }
  }
  q.push(make_tuple(starti, startj, K + 1));
  while (!q.empty()) {
    tuple<int, int, int> t = q.front();
    q.pop();
    int i = get<0>(t);
    int j = get<1>(t);
    s.insert(make_pair(i, j));
    int k = get<2>(t);
    k--;
    if (k == 0) continue;
    if (i - 1 >= 0 && c[i - 1][j] == '.' &&
        s.find(make_pair(i - 1, j)) == s.end())
      q.push(make_tuple(i - 1, j, k));
    if (i + 1 < h && c[i + 1][j] == '.' &&
        s.find(make_pair(i + 1, j)) == s.end())
      q.push(make_tuple(i + 1, j, k));
    if (j - 1 >= 0 && c[i][j - 1] == '.' &&
        s.find(make_pair(i, j - 1)) == s.end())
      q.push(make_tuple(i, j - 1, k));
    if (j + 1 < w && c[i][j + 1] == '.' &&
        s.find(make_pair(i, j + 1)) == s.end())
      q.push(make_tuple(i, j + 1, k));
  }
  for (auto it = s.begin(); it != s.end(); ++it) {
    ans = min(ans, (int)ceil(1.0 * it->first / K) + 1);
    ans = min(ans, (int)ceil(1.0 * it->second / K) + 1);
    ans = min(ans, (int)ceil(1.0 * (h - 1 - it->first) / K) + 1);
    ans = min(ans, (int)ceil(1.0 * (w - 1 - it->second) / K) + 1);
  }
  cout << ans;
  return 0;
}
