#include <bits/stdc++.h>
using namespace std;
int H, W, K;
string A[810];
int mini[810][810];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool isValid(int x, int y) { return 0 <= x && x < W && 0 <= y && y < H; }
struct Data {
  int x, y, v;
  bool operator<(const Data &data) const {
    if (v != data.v) return v > data.v;
    if (x != data.x) return x > data.x;
    return y > data.y;
  }
};
void compute() {
  priority_queue<Data> Q;
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j) mini[i][j] = INT_MAX;
  {
    int sx = -1, sy;
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        if (A[i][j] == 'S') {
          A[i][j] = '.';
          sx = j, sy = i;
          mini[i][j] = 0;
          goto Skip;
        }
  Skip:
    assert(sx != -1);
    Q.push((Data){sx, sy, 0});
  }
  while (!Q.empty()) {
    Data data = Q.top();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = data.x + dx[i];
      int ny = data.y + dy[i];
      if (!isValid(nx, ny)) continue;
      int nv = data.v;
      if (A[ny][nx] == '#') ++nv;
      if (mini[ny][nx] > nv) {
        mini[ny][nx] = nv;
        Q.push((Data){nx, ny, nv});
      }
    }
  }
  int ans = INT_MAX;
  for (int i = 0; i < H; ++i) {
    int cost = ceil(mini[i][0] / (double)K) + 1;
    ans = min(ans, cost);
    cost = ceil(mini[i][W - 1] / (double)K) + 1;
    ans = min(ans, cost);
  }
  for (int i = 0; i < W; ++i) {
    int cost = ceil(mini[0][i] / (double)K) + 1;
    ans = min(ans, cost);
    cost = ceil(mini[H - 1][i] / (double)K) + 1;
    ans = min(ans, cost);
  }
  cout << ans << endl;
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; ++i) cin >> A[i];
  compute();
  return 0;
}
