#include <bits/stdc++.h>
using namespace std;
int seen[1000][1000];
char g[1000][1000];
int dist(int a, int b, int x, int y) { return abs(a - x) + abs(b - y); }
int main(void) {
  int n, m, k;
  scanf("%i %i %i", &n, &m, &k);
  int sx = 0, sy = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf(" %c", &g[i][j]);
      if (g[i][j] == 'S') {
        sx = i, sy = j;
      }
    }
  }
  vector<pair<int, int>> a, b;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i == 1 || i == n || j == 1 || j == m) {
        b.push_back(make_pair(i, j));
      }
    }
  }
  queue<int> Q;
  Q.push(sx), Q.push(sy);
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    int y = Q.front();
    Q.pop();
    a.push_back(make_pair(x, y));
    int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
    for (int j = 0; j < 4; j++) {
      int nx = x + dx[j], ny = y + dy[j];
      if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && !seen[nx][ny] &&
          g[nx][ny] != '#' && dist(nx, ny, sx, sy) <= k) {
        seen[nx][ny] = 1;
        Q.push(nx);
        Q.push(ny);
      }
    }
  }
  int ans = 10000000;
  for (auto s : a) {
    for (auto e : b) {
      int d = dist(s.first, s.second, e.first, e.second);
      ans = min(ans, (d + k - 1) / k + 1);
    }
  }
  printf("%i\n", ans);
  return 0;
}
