#include <bits/stdc++.h>
using namespace std;
long long H, W, K;
long long A[805][805];
long long WORK[805][805];
map<pair<long long, long long>, vector<pair<long long, long long> > > way;
bool valid(long long h, long long w) {
  return (h >= 0) && (h < H) && (w >= 0) && (w < W);
}
void dik(long long h, long long w) {
  priority_queue<pair<long long, pair<long long, long long> >,
                 vector<pair<long long, pair<long long, long long> > >,
                 greater<pair<long long, pair<long long, long long> > > >
      Q;
  Q.push(make_pair(0, make_pair(h, w)));
  while (!Q.empty()) {
    long long cost = Q.top().first;
    long long curh = Q.top().second.first;
    long long curw = Q.top().second.second;
    pair<long long, long long> curpair = make_pair(curh, curw);
    Q.pop();
    A[curh][curw] = min(A[curh][curw], cost);
    for (long long i = 0; i < way[curpair].size(); i++) {
      long long nexth = way[curpair][i].first;
      long long nextw = way[curpair][i].second;
      long long nextcost = cost + 1;
      if (A[nexth][nextw] >= nextcost) {
        Q.push(make_pair(nextcost, make_pair(nexth, nextw)));
      }
    }
  }
}
void firstmove() {
  for (long long h = 0; h < H; h++) {
    for (long long w = 0; w < W; w++) {
      if (A[h][w] == 0) {
        dik(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;
      }
    }
  }
  for (long long h = 0; h < H; h++) {
    for (long long w = 0; w < W; w++) {
      if (A[h][w] != LONG_MAX / 3) {
        if (valid(h - 1, w) && A[h - 1][w] != LONG_MAX / 3) {
          way[make_pair(h, w)].push_back(make_pair(h - 1, w));
        }
        if (valid(h + 1, w) && A[h + 1][w] != LONG_MAX / 3) {
          way[make_pair(h, w)].push_back(make_pair(h + 1, w));
        }
        if (valid(h, w - 1) && A[h][w - 1] != LONG_MAX / 3) {
          way[make_pair(h, w)].push_back(make_pair(h, w - 1));
        }
        if (valid(h, w + 1) && A[h][w + 1] != LONG_MAX / 3) {
          way[make_pair(h, w)].push_back(make_pair(h, w + 1));
        }
      }
    }
  }
  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;
}
