#include <bits/stdc++.h>
using namespace std;
using ll = long long;
char f[1000][1000];
int d[1000][1000];
const int inf = 1e8;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int sx = 0, sy = 0;
  for (int(i) = (0); (i) < (int)(H); ++(i)) {
    for (int(j) = (0); (j) < (int)(W); ++(j)) {
      cin >> f[i][j];
      if (f[i][j] == 'S') sy = i, sx = j;
      d[i][j] = inf;
    }
  }
  queue<pair<int, int> > que, pos;
  que.push({sy, sx});
  d[sy][sx] = 0;
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int(dd) = (0); (dd) < (int)(4); ++(dd)) {
      int px = p.second + dx[dd];
      int py = p.first + dy[dd];
      if (0 <= px && px < W && 0 <= py && py < H && f[py][px] == '.' &&
          d[py][px] == inf) {
        que.push({py, px});
        d[py][px] = d[p.first][p.second] + 1;
      }
    }
  }
  int ans = inf;
  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      if (d[y][x] != inf) {
        int Y = min(y, H - y - 1);
        int X = min(x, W - x - 1);
        ans = min(ans, min(Y / K + bool(Y % K), X / K + bool(X % K)));
      }
    }
  }
  cout << ans + 1 << endl;
}
