#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, a[805][805], u[805][805];
int o[] = {1, -1, 0, 0};
int p[] = {0, 0, -1, 1};
char orz;
bool git(int i, int j) {
  return !(i < 1 or j < 1 or i > n or j > n or u[i][j] or a[i][j]);
}
bool dene(int z) {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) u[i][j] = 0;
  u[x][y] = 1;
  queue<pair<int, pair<int, int> > > q;
  q.push(make_pair(k, make_pair(x, y)));
  int mn = 805;
  while (!q.empty()) {
    int kal = q.front().first;
    int i = q.front().second.first;
    int j = q.front().second.second;
    q.pop();
    mn = min(mn, i - 1);
    mn = min(mn, j - 1);
    mn = min(mn, n - i);
    mn = min(mn, m - j);
    if (!kal) continue;
    for (int k = 0; k < 4; k++)
      if (git(i + o[k], j + p[k])) {
        q.push(make_pair(kal - 1, make_pair(i + o[k], j + p[k])));
        u[i + o[k]][j + p[k]] = 1;
      }
  }
  int gt = mn / k + (mn % k != 0) + 1;
  return (gt <= z);
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf(" %c", &orz);
      if (orz == 'S') {
        x = i;
        y = j;
      }
      if (orz == '#') a[i][j] = 1;
    }
  int bas = 1, son = n * m + 2;
  while (bas < son) {
    if (dene(((bas + son) / 2)))
      son = ((bas + son) / 2);
    else
      bas = ((bas + son) / 2) + 1;
  }
  printf("%d\n", ((bas + son) / 2));
  return 0;
}
