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