#include <bits/stdc++.h>
using namespace std;
long long H, W, K;
long long room[800][800];
long long GetCnt(long long x, long long y) {
  long long d1 = (x - 1) / K;
  long long d2 = (y - 1) / K;
  long long d3 = ((W - 1) - (x + 1)) / K;
  long long d4 = ((H - 1) - (y + 1)) / K;
  return (((d1) < ((((d2) < ((((d3) < (d4)) ? (d3) : (d4))))
                        ? (d2)
                        : ((((d3) < (d4)) ? (d3) : (d4))))))
              ? (d1)
              : ((((d2) < ((((d3) < (d4)) ? (d3) : (d4))))
                      ? (d2)
                      : ((((d3) < (d4)) ? (d3) : (d4)))))) +
         1;
}
signed main() {
  long long sx, sy;
  cin >> H >> W >> K;
  for (long long y = 0; y < H; y++) {
    string str;
    cin >> str;
    for (long long x = 0; x < W; x++) {
      room[x][y] = (str.at(x) == '#') ? 1 : 0;
      if (str.at(x) == 'S') {
        sx = x;
        sy = y;
      }
    }
  }
  if (sx == 0 || sy == 0 || sx == W - 1 || sy == H - 1) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = GetCnt(sx, sy);
  long long dx[4] = {1, 0, -1, 0};
  long long dy[4] = {0, 1, 0, -1};
  for (long long d = 0; d < 4; d++) {
    long long x = sx;
    long long y = sy;
    for (long long i = 0; i < K; i++) {
      x += dx[d];
      y += dy[d];
      if (room[x][y] == 1) {
        break;
      }
      if (x == 0 || y == 0 || x == W - 1 || y == H - 1) {
        cout << 1 << endl;
        return 0;
      }
      ans = (((ans) < (GetCnt(x, y))) ? (ans) : (GetCnt(x, y)));
    }
  }
  cout << (ans + 1) << endl;
}
