#include <bits/stdc++.h>
using namespace std;
int h, w, k, x, y, mn[805][805], ans;
char mat[805][805];
int dx[] = {0, 1, -1, 0};
int dy[] = {1, 0, 0, -1};
bool OK(int x, int y, bool free) {
  if (x > h || y > w || x <= 0 || y <= 0) return 0;
  if (mat[x][y] == '#' && !free) return 0;
  return 1;
}
vector<pair<int, int> > Q;
void prec() {
  Q.push_back({x, y});
  mn[x][y] = 1;
  for (int i = 0; i < Q.size(); i++) {
    if (Q[i].first == 1 || Q[i].first == h || Q[i].second == 1 ||
        Q[i].second == w) {
      cout << 1 << '\n';
      exit(0);
    }
    for (int j = 0; j < 4; j++) {
      int X = Q[i].first + dx[j];
      int Y = Q[i].second + dy[j];
      if (OK(X, Y, 0)) {
        if (mn[X][Y] == 0 || mn[X][Y] > mn[Q[i].first][Q[i].second] + 1 &&
                                 mn[Q[i].first][Q[i].second] + 1 <= k + 1) {
          Q.push_back({X, Y});
          mn[X][Y] = mn[Q[i].first][Q[i].second] + 1;
        }
      }
    }
  }
}
int OK() {
  vector<pair<int, int> > H;
  memset(mn, 0, sizeof(mn));
  for (auto it : Q) {
    H.push_back(it);
    mn[it.first][it.second] = 1;
  }
  for (int i = 0; i < H.size(); i++) {
    if (H[i].first == 1 || H[i].second == 1 || H[i].first == h ||
        H[i].second == w) {
      return ((mn[H[i].first][H[i].second] + k - 2) / k) + 1;
    }
    for (int j = 0; j < 4; j++) {
      int X = H[i].first + dx[j];
      int Y = H[i].second + dy[j];
      if (OK(X, Y, 1)) {
        if (mn[X][Y] == 0 || mn[X][Y] > mn[H[i].first][H[i].second] + 1) {
          H.push_back({X, Y});
          mn[X][Y] = mn[H[i].first][H[i].second] + 1;
        }
      }
    }
  }
}
int main() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    cin >> mat[i] + 1;
    for (int j = 1; j <= w; j++) {
      if (mat[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  prec();
  cout << OK();
  return 0;
}
