#include <bits/stdc++.h>
using namespace std;
const int NMAX = 800;
char a[NMAX + 5][NMAX + 5];
bool vis[NMAX + 5][NMAX + 5];
queue<pair<int, int> > q;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] == 'S') {
        q.push(pair<int, int>(i, j));
        vis[i][j] = true;
      }
  int Min = (1LL << 31) - 1;
  while (!q.empty()) {
    pair<int, int> top = q.front();
    q.pop();
    Min = min(Min, top.first);
    Min = min(Min, top.second);
    Min = min(Min, n - 1 - top.first);
    Min = min(Min, m - 1 - top.second);
    for (int i = 0; i < 4; i++) {
      pair<int, int> New =
          pair<int, int>(top.first + dx[i], top.second + dy[i]);
      if (New.first >= 0 && New.second >= 0 && New.first < n &&
          New.second < m && vis[New.first][New.second] == 0)
        if (a[New.first][New.second] == '.') {
          vis[New.first][New.second] = true;
          q.push(New);
        }
    }
  }
  if (Min == 0)
    printf("1\n");
  else
    printf("%d\n", 2 + (Min - 1) / k);
  return 0;
}
