#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int n, m, k, b = 0, c[1001][1001], u, z, vs = 0, h1 = 1e9, h2 = 1e9;
pair<int, int> v[702020];
char a[1001][1001];
void dfs(int x, int y) {
  if (b > k) {
    b--;
    return;
  }
  b++;
  c[x][y] = 1;
  for (int i = (int)0; i < (int)4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] == '#') continue;
    dfs(nx, ny);
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = (int)0; i < (int)n; i++)
    for (int j = (int)0; j < (int)m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') u = i, z = j;
      c[i][j] = 0;
    }
  dfs(u, z);
  for (int i = (int)0; i < (int)n; i++)
    for (int j = (int)0; j < (int)m; j++)
      if (c[i][j]) v[vs++] = make_pair(i, j);
  for (int i = (int)0; i < (int)vs; i++) {
    h1 = min(min(v[i].first + 1, n - v[i].first - 1), h1);
    h2 = min(min(v[i].second + 1, m - v[i].second - 1), h2);
  }
  cout << (min(h1, h2) + k - 1) / k + 1;
  return 0;
}
