#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char second[808][808];
int d[808][808];
queue<pair<int, int> > q;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%s", second[i]);
  }
  memset(d, -1, sizeof(d));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (second[i][j] == 'S') {
        d[i][j] = 0;
        q.push(make_pair(i, j));
      }
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    if (d[x][y] == k) continue;
    for (int dx = -1; dx <= 1; ++dx)
      for (int dy = -1; dy <= 1; ++dy) {
        if (abs(dx) + abs(dy) != 1) continue;
        int sx = x + dx;
        int sy = y + dy;
        if (sx < 0 || sx >= n || sy < 0 || sy >= m || second[sx][sy] == '#' ||
            d[sx][sy] != -1)
          continue;
        d[sx][sy] = d[x][y] + 1;
        q.push(make_pair(sx, sy));
      }
  }
  int ans = int(1e9);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (d[i][j] == -1) continue;
      ans = min(ans, (i + k - 1) / k + 1);
      ans = min(ans, (j + k - 1) / k + 1);
      ans = min(ans, (n - 1 - i + k) / k + 1);
      ans = min(ans, (m - 1 - j + k) / k + 1);
    }
  printf("%d\n", ans);
  return 0;
}
