#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int h, w, k, stx, sty, dist[805][805], x[4] = {0, 0, 1, -1},
                                       y[4] = {1, -1, 0, 0}, mini = 1e9;
char g[805][805];
void ff(int a, int b) {
  if (a == 1 || a == h || b == 1 || b == w) {
    return;
  }
  for (int i = 0; i < 4; i++) {
    if (g[a + x[i]][b + y[i]] == '.' && dist[a + x[i]][b + y[i]] == -1 &&
        dist[a][b] < k) {
      dist[a + x[i]][b + y[i]] = dist[a][b] + 1;
      ff(a + x[i], b + y[i]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(dist, -1, sizeof(dist));
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> g[i][j];
      if (g[i][j] == 'S') {
        stx = i;
        sty = j;
      }
    }
  }
  dist[stx][sty] = 0;
  ff(stx, sty);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (dist[i][j] != -1) {
        if (i == 1 || i == h || j == 1 || j == w) {
          mini = 0;
        }
        mini = min(mini, (max(0, i - 1 - 1)) / k + 1);
        mini = min(mini, (max(0, h - i - 1)) / k + 1);
        mini = min(mini, (max(0, j - 1 - 1)) / k + 1);
        mini = min(mini, (max(0, w - j - 1)) / k + 1);
      }
    }
  }
  cout << mini + 1 << endl;
}
