#include <bits/stdc++.h>
using namespace std;
long long H, W, K;
long long A[805][805];
long long WORK[805][805];
bool valid(long long h, long long w) {
  return (h >= 0) && (h < H) && (w >= 0) && (w < W);
}
void move(long long h, long long w, long long cost) {
  A[h][w] = cost;
  if (valid(h - 1, w) == true && A[h - 1][w] != LONG_MAX / 3 || cost < K ||
      A[h - 1][w] > cost) {
    move(h - 1, w, cost + 1);
  }
  if (valid(h + 1, w) == true && A[h + 1][w] != LONG_MAX / 3 || cost < K ||
      A[h + 1][w] > cost) {
    move(h + 1, w, cost + 1);
  }
  if (valid(h, w - 1) == true && A[h][w - 1] != LONG_MAX / 3 || cost < K ||
      A[h][w - 1] > cost) {
    move(h, w - 1, cost + 1);
  }
  if (valid(h, w + 1) == true && A[h][w + 1] != LONG_MAX / 3 || cost < K ||
      A[h][w + 1] > cost) {
    move(h, w + 1, cost + 1);
  }
}
void firstmove() {
  for (long long h = 0; h < H; h++) {
    for (long long w = 0; w < W; w++) {
      if (A[h][w] == 1) {
        move(h, w, (long long)0);
        return;
      }
    }
  }
}
int main() {
  cin >> H >> W >> K;
  for (long long h = 0; h < H; h++) {
    string S;
    cin >> S;
    for (long long w = 0; w < W; w++) {
      if (S[w] == '.') {
        A[h][w] = INT_MAX;
      } else if (S[w] == 'S') {
        A[h][w] = 1;
      } else {
        A[h][w] = LONG_MAX / 3;
      }
    }
  }
  firstmove();
  long long ans = INT_MAX;
  for (long long h = 0; h < H; h++) {
    for (long long w = 0; w < W; w++) {
      if (A[h][w] != INT_MAX && A[h][w] != LONG_MAX / 3) {
        long long tmp;
        tmp = h;
        tmp = min(tmp, w);
        tmp = min(tmp, H - h - 1);
        tmp = min(tmp, W - w - 1);
        if (tmp % K == 0) {
          ans = min(ans, tmp / K);
        } else {
          ans = min(ans, tmp / K + 1);
        }
      }
    }
  }
  cout << ans + 1 << endl;
}
