#include <bits/stdc++.h>
using namespace std;
const int dy[4] = {1, 0, -1, 0};
const int dx[4] = {0, 1, 0, -1};
int H, W, K;
class Coord {
 public:
  int y, x, k;
  vector<Coord> nextCoord;
  Coord(int i, int j, int _k) {
    y = i;
    x = j;
    k = _k;
  }
  void setNextCoord() {
    for (int i = 0; i < 4; i++) {
      if (k + 1 > K) continue;
      nextCoord.push_back(Coord(y + dy[i], x + dx[i], k + 1));
    }
  }
  int hash() { return y * W + x; }
  int remain() { return min(min(y, H - y - 1), min(x, W - x - 1)); }
};
int main() {
  cin >> H >> W >> K;
  vector<vector<char> > A(H, vector<char>(W, '#'));
  unordered_set<int> st;
  queue<Coord> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') que.push(Coord(i, j, 0));
    }
  }
  int minRemain = 801;
  while (!que.empty()) {
    Coord c = que.front();
    que.pop();
    st.insert(c.hash());
    c.setNextCoord();
    minRemain = c.remain() < minRemain ? c.remain() : minRemain;
    for (Coord nc : c.nextCoord) {
      if (st.find(nc.hash()) != st.end()) continue;
      if (A[nc.y][nc.x] == '#') continue;
      que.push(nc);
    }
    if (minRemain == 0) break;
  }
  cout << 1 + (minRemain + K - 1) / K << endl;
  return 0;
}
