#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int n, m, k, d[805][805], a[805][805];
char s[805];
pair<int, int> start;
int min4(int u, int v, int p, int q) { return min(min(u, v), min(p, q)); }
bool valid(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; ++j) {
      a[i][j] = s[j] == '#';
      if (s[j] == 'S') start = pair<int, int>(i, j);
    }
  }
  memset(d, -1, sizeof(d));
  int res = (int)1e9 + 7;
  queue<pair<int, int> > q;
  q.push(pair<int, int>(start.first, start.second)),
      d[start.first][start.second] = 0;
  while (((int)q.size())) {
    int u = q.front().first, v = q.front().second;
    q.pop();
    res = min(res, (min4(u - 1, v - 1, n - u, m - v) - 1) / k + 2);
    if (d[u][v] == k) continue;
    for (int i = 0; i < 4; ++i) {
      int x = u + dx[i], y = v + dy[i];
      if (!valid(x, y) || d[x][y] != -1 || a[x][y]) continue;
      d[x][y] = d[u][v] + 1;
      q.push(pair<int, int>(x, y));
    }
  }
  printf("%d", res);
  return 0;
}
