#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,
             h[1001][1001];
deque<pair<int, int> > q;
char a[1001][1001];
bool ad(int x, int y) {
  return x + 1 && x < n && y + 1 && y < m && a[x][y] != '#' && !h[x][y];
}
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];
      c[i][j] = 1;
      if (a[i][j] == 'S') u = i, z = j;
      h[i][j] = 0;
    }
  if (u == 0 || u == n - 1 || z == 0 || z == m - 1) {
    cout << "0";
    return 0;
  }
  q.push_front(make_pair(u, z));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    h[x][y] = 1;
    if (c[x][y] - 1 <= k)
      h1 = min(min(min(y, m - y - 1), h2), min(min(x, n - x - 1), h1));
    else
      break;
    for (int i = (int)0; i < (int)4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (ad(nx, ny)) {
        q.push_back(make_pair(nx, ny));
        c[nx][ny] += c[x][y];
      }
    }
  }
  cout << (h1 + k - 1) / k + 1 << endl;
  return 0;
}
