#include <bits/stdc++.h>
using namespace std;
int a[800 + 2][800 + 2];
char s[800 + 1];
queue<pair<int, int> > Q;
int minim(int a, int b) { return (a < b) ? a : b; }
int main() {
  int n, m, k;
  int i, j;
  scanf("%d%d%d\n", &n, &m, &k);
  pair<int, int> S;
  for (i = 1; i <= n; i++) {
    gets(s);
    for (j = 0; j < m; j++) {
      if (s[j] == 'S')
        S = {i, j + 1};
      else if (s[j] == '#')
        a[i][j + 1] = -1;
    }
  }
  for (i = 1; i <= n; i++) a[i][0] = a[i][m + 1] = -1;
  for (j = 1; j <= m; j++) a[0][j] = a[n + 1][j] = -1;
  a[S.first][S.second] = 1;
  Q.push({S.first, S.second});
  int min = n;
  while (!Q.empty()) {
    S = Q.front();
    Q.pop();
    min = minim(min,
                minim(S.first - 1,
                      minim(S.second - 1, minim(n - S.first, m - S.second))));
    if (a[S.first][S.second] <= k) {
      if (a[S.first + 1][S.second] == 0) {
        a[S.first + 1][S.second] = a[S.first][S.second] + 1;
        Q.push({S.first + 1, S.second});
      }
      if (a[S.first - 1][S.second] == 0) {
        a[S.first - 1][S.second] = a[S.first][S.second] + 1;
        Q.push({S.first - 1, S.second});
      }
      if (a[S.first][S.second + 1] == 0) {
        a[S.first][S.second + 1] = a[S.first][S.second] + 1;
        Q.push({S.first, S.second + 1});
      }
      if (a[S.first][S.second - 1] == 0) {
        a[S.first][S.second - 1] = a[S.first][S.second] + 1;
        Q.push({S.first, S.second - 1});
      }
    }
  }
  printf("%d", 1 + (min - 1) / k + 1);
  return 0;
}
