#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int SI, SJ;
bool U[800][800];
int DI[4] = {-1, 0, 1, 0};
int DJ[4] = {0, -1, 0, 1};
int R = INT_MAX;
void see(int i, int j) {
  U[i][j] = true;
  R = min({R, i, H - 1 - i, j, W - 1 - j});
  for (int x = 0; x < 4; ++x) {
    int di = DI[x], dj = DJ[x];
    if (i + di < 0 || i + di >= H || j + dj < 0 || j + dj >= W) continue;
    if (U[i + di][j + dj]) continue;
    see(i + di, j + dj);
  }
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j) {
      char c;
      cin >> c;
      U[i][j] = c != '.';
      if (c == 'S') {
        SI = i;
        SJ = j;
      }
    }
  see(SI, SJ);
  cout << 1 + (R + K - 1) / K << endl;
  return 0;
}
