#include <bits/stdc++.h>
using namespace std;
const int N = 800 + 10, inf = 1e6;
string grid[N];
int n, m, k, dist[N][N];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
bool ok(int x, int y) { return 1 ^ (x < 0 || y < 0 || x >= n || y >= m); }
void bfs(int x, int y) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) dist[i][j] = N;
  }
  dist[x][y] = 0;
  queue<pair<int, int> > q;
  q.push({x, y});
  while (!q.empty()) {
    pair<int, int> e = q.front();
    q.pop();
    x = e.first, y = e.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (ok(nx, ny) && dist[nx][ny] == N) {
        dist[nx][ny] = dist[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  int x, y;
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == 'S') x = i, y = j;
    }
  }
  bfs(x, y);
  int ans = N;
  if (x == 0 || y == 0 || x == n - 1 || y == m - 1) ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[i][j] > k) continue;
      ans = min(ans, (int)ceil((double)(i) / k) + 1);
      ans = min(ans, (int)ceil((double)(j) / k) + 1);
      ans = min(ans, (int)ceil((double)(n - i - 1) / k) + 1);
      ans = min(ans, (int)ceil((double)(m - j - 1) / k) + 1);
    }
  }
  cout << ans << "\n";
}
