#include <bits/stdc++.h>
using namespace std;
char pet[803][803];
bool visited[803][803];
int mox[] = {0, 0, -1, 1}, moy[] = {-1, 1, 0, 0};
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i <= n + 1; i++)
    for (int j = 0; j <= m + 1; j++) pet[i][j] = '#';
  pair<int, int> s;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> pet[i][j];
      if (pet[i][j] == 'S') s = {i, j};
    }
  }
  queue<pair<pair<int, int>, int> > q;
  q.push({s, k});
  visited[s.first][s.second] = 1;
  while (q.size()) {
    pair<pair<int, int>, int> curr = q.front();
    q.pop();
    int rem = curr.second;
    int x = curr.first.first;
    int y = curr.first.second;
    if (rem == 0) continue;
    for (int i = 0; i < 4; i++) {
      int _x = x + mox[i];
      int _y = y + moy[i];
      if (visited[_x][_y] || pet[_x][_y] == '#') continue;
      visited[_x][_y] = 1;
      q.push({{_x, _y}, rem - 1});
    }
  }
  int mn = 1e9;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (visited[i][j]) {
        int jar = min({i - 1, j - 1, n - i, m - i});
        jar = (jar + k - 1) / k;
        mn = min(jar + 1, mn);
      }
    }
  }
  cout << mn << "\n";
}
