#include <bits/stdc++.h>
using namespace std;
const int N = 800;
int n, m, k;
bool used[N][N];
char p[N][N];
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int d[N][N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) p[i][j] = getchar();
    getchar();
  }
  queue<pair<int, int> > Q;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (p[i][j] == 'S') Q.push({i, j}), used[i][j] = true;
  while (!Q.empty()) {
    int x = Q.front().first, y = Q.front().second;
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      int tox = x + dx[i], toy = y + dy[i];
      if (0 <= tox && tox < n && 0 <= toy && toy < m && !used[tox][toy] &&
          p[tox][toy] != '#') {
        used[tox][toy] = true;
        d[tox][toy] = d[x][y] + 1;
        Q.push({tox, toy});
      }
    }
  }
  int ans = 1000000000;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (used[i][j] && d[i][j] <= k) {
        ans = min(ans, 1 + (i + k - 1) / k);
        ans = min(ans, 1 + (n - 1 - i + k - 1) / k);
        ans = min(ans, 1 + (j + k - 1) / k);
        ans = min(ans, 1 + (m - 1 - j + k - 1) / k);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
