#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 orih, long long oriw) {
  queue<pair<long long, pair<long long, long long> > > Q;
  set<pair<long long, long long> > done;
  Q.push(make_pair(0, make_pair(orih, oriw)));
  while (Q.size() > 0 && Q.front().first <= K) {
    long long cost = Q.front().first;
    long long h = Q.front().second.first;
    long long w = Q.front().second.second;
    done.insert(make_pair(h, w));
    A[h][w] = cost;
    Q.pop();
    if (valid(h - 1, w) == true && A[h - 1][w] != LONG_MAX / 3 &&
        done.count(make_pair(h - 1, w)) == 0) {
      Q.push(make_pair(cost + 1, make_pair(h - 1, w)));
    }
    if (valid(h + 1, w) == true && A[h + 1][w] != LONG_MAX / 3 &&
        done.count(make_pair(h + 1, w)) == 0) {
      Q.push(make_pair(cost + 1, make_pair(h + 1, w)));
    }
    if (valid(h, w - 1) == true && A[h][w - 1] != LONG_MAX / 3 &&
        done.count(make_pair(h, w - 1)) == 0) {
      Q.push(make_pair(cost + 1, make_pair(h, w - 1)));
    }
    if (valid(h, w + 1) == true && A[h][w + 1] != LONG_MAX / 3 &&
        done.count(make_pair(h, w + 1)) == 0) {
      Q.push(make_pair(cost + 1, make_pair(h, w + 1)));
    }
  }
}
void firstmove() {
  for (long long h = 0; h < H; h++) {
    for (long long w = 0; w < W; w++) {
      if (A[h][w] == 0) {
        move(h, w);
        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] = 0;
      } 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;
}
