#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];
int sr[10000000], sc[10000000], ss;
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;
    }
  sr[ss] = Sr, sc[ss++] = Sc;
  while (ss) {
    ss--;
    int r = sr[ss], c = sc[ss];
    if (r < 1 || c < 1 || r > R || c > C || seen[r][c] || !m[r][c]) continue;
    seen[r][c] = 1;
    if (r < minr) minr = r;
    if (r > maxr) maxr = r;
    if (c < minc) minc = c;
    if (c > maxc) maxc = c;
    sr[ss] = r + 1;
    sc[ss++] = c;
    sr[ss] = r - 1;
    sc[ss++] = c;
    sr[ss] = r;
    sc[ss++] = c + 1;
    sr[ss] = r;
    sc[ss++] = c - 1;
  }
  int d = min(min(minr - 1, minc - 1), min(R - maxr, C - maxc));
  printf("%d\n", 1 + (d + K - 1) / K);
}
