#include <bits/stdc++.h>
using namespace std;
int R, C, K, Sr, Sc;
bool m[805][805];
int minr = 1e9, maxr, minc = 1e9, maxc;
bool seen[805][805];
void fill(int r, int c) {
  if (r < 1 || c < 1 || r > R || c > C || seen[r][c] || !m[r][c]) return;
  seen[r][c] = 1;
  if (r < minr) minr = r;
  if (r > maxr) maxr = r;
  if (c < minc) minc = c;
  if (c > maxc) maxc = c;
  fill(r + 1, c);
  fill(r - 1, c);
  fill(r, c + 1);
  fill(r, c - 1);
}
int main() {
  scanf("%d %d %d", &R, &C, &K);
  char s;
  for (int r = 1; r <= R; r++)
    for (int c = 1; c <= C; c++) {
      scanf(" %c", &s);
      m[r][c] = 1;
      if (s == '#')
        m[r][c] = 0;
      else if (s == 'S')
        Sr = r, Sc = c;
    }
  fill(Sr, Sc);
  int d = min(min(minr - 1, minc - 1), min(R - maxr, C - maxc));
  printf("%d\n", 1 + (d + K - 1) / K);
}
