#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
int n, m, k;
bool used[805][805];
char a[805][805];
queue<pair<pair<int, int>, int> > q;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  memset(used, false, sizeof(used));
  for (int i = 0; i < n; i++) {
    scanf("%s", a[i]);
    for (int j = 0; j < m; j++)
      if (a[i][j] == 'S') {
        a[i][j] = '.';
        q.push(make_pair(make_pair(i, j), 0));
        used[i][j] = true;
      }
  }
  while (!q.empty()) {
    int x, y, d;
    x = q.front().first.first, y = q.front().first.second, d = q.front().second;
    q.pop();
    if (!x || !y || x == n - 1 || y == m - 1) {
      cout << 1 << endl;
      return 0;
    }
    if (d == k) continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx >= 0 && ny >= 0 && nx < n && ny < m && a[nx][ny] == '.' &&
          !used[nx][ny]) {
        q.push(make_pair(make_pair(nx, ny), d + 1));
        used[nx][ny] = true;
      }
    }
  }
  int ans = 2147483647;
  for (int i = 1; i < n - 1; i++) {
    for (int j = 1; j < m - 1; j++) {
      if (used[i][j]) {
        int x = min(min(i, j), min(n - i - 1, n - j - 1));
        x = (x + k - 1) / k;
        ans = min(ans, x);
      }
    }
  }
  if (ans + 1 < 0)
    for (;;)
      ;
  printf("%d\n", ans);
  return 0;
}
