#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
queue<pair<int, int> > que;
int main() {
  int h, w, k, sx, sy, ans;
  scanf("%d %d %d", &h, &w, &k);
  vector<vector<char> > c(h, vector<char>(w));
  vector<vector<int> > d(h, vector<int>(w));
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      d[i][j] = 30000;
    }
  }
  for (int i = 0; i < h; i++) {
    scanf("\n");
    for (int j = 0; j < w; j++) {
      scanf("%c", &c[j][i]);
      if (c[j][i] == 'S') {
        d[j][i] = 0;
        sx = j;
        sy = i;
        que.push(pair<int, int>(j, i));
      }
    }
  }
  while (!que.empty()) {
    int x, y;
    int x1, y1;
    int a;
    tie(x, y) = que.front();
    a = d[x][y];
    que.pop();
    for (int i = 0; i < 4; i++) {
      x1 = x + dx[i];
      y1 = y + dy[i];
      if (x1 >= w) continue;
      if (y1 >= h) continue;
      if (x1 < 0) continue;
      if (y1 < 0) continue;
      if (c[x1][y1] == '.') {
        if (d[x1][y1] == 30000) {
          d[x1][y1] = a + 1;
          que.push(pair<int, int>(x1, y1));
        }
      }
    }
  }
  int dis, dis1, dis2;
  int min_out = 30000;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dis1 = min(i, j);
      dis2 = min((h - i), (w - j));
      dis = min(dis1, dis2);
      if (k >= d[j][i] & min_out > dis) {
        min_out = dis;
      }
    }
  }
  if (sx == 0 | sx == w - 1 | sy == 0 | sy == h - 1) {
    printf("0\n");
    return 0;
  }
  ans = 1;
  while (1) {
    if (min_out <= 0) {
      printf("%d\n", ans);
      return 0;
    }
    min_out -= k;
    ans++;
  }
}
