#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  char A[810][810];
  int sx, sy;
  for (int i = (0); i < (int)H; i++)
    for (int j = (0); j < (int)W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        sx = j;
        sy = i;
      }
    }
  int mn = min(abs(sx - 0), abs(W - 1 - sx));
  mn = min(mn, min(abs(sy - 0), abs(H - 1 - sy)));
  for (int i = (max(0, sy - K)); i < (int)min(H, sy + K + 1); i++) {
    for (int j = (max(0, sx - K)); j < (int)min(H, sx + K + 1); j++) {
      int dy = sy - i, dx = sx - j;
      if (A[i][j] == '#' || dy + dx > K) continue;
      if ((dy + dx) % 2 == K % 2) {
        mn = min(mn, min(abs(j - 0), abs(W - 1 - j)));
        mn = min(mn, min(abs(i - 0), abs(H - 1 - i)));
      }
    }
  }
  int ans = 1;
  while (mn > 0) {
    ans++;
    mn -= K;
  }
  cout << (ans) << endl;
  return 0;
}
