#include <bits/stdc++.h>
using namespace std;
const int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
char a[810][810];
int d[810][810];
pair<int, int> q[640010];
int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) d[i][j] = k + 1;
  int f = 1, r = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 'S') {
        q[1] = make_pair(i, j);
        d[i][j] = 0;
      }
  while (f <= r && d[q[f].first][q[f].second] < k) {
    for (int k = 0; k < 4; k++) {
      int dx = q[f].first + di[k][0], dy = q[f].second + di[k][1];
      if (dx > 0 && dx <= n && dy > 0 && dy <= m && d[dx][dy] == k + 1 &&
          a[dx][dy] == '.') {
        q[++r] = make_pair(dx, dy);
        d[dx][dy] = d[q[f].first][q[f].second] + 1;
      }
    }
    f++;
  }
  int ans = n * m;
  for (int i = 1; i <= r; i++) {
    ans = min(ans, (q[i].first - 1 + k - 1) / k);
    ans = min(ans, (q[i].second - 1 + k - 1) / k);
    ans = min(ans, (n - q[i].first + k - 1) / k);
    ans = min(ans, (m - q[i].second + k - 1) / k);
  }
  printf("%d\n", ans + 1);
  return 0;
}
