#include <bits/stdc++.h>
using namespace std;
void preprocess(void) { return; }
string ar[850];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(20);
  preprocess();
  int H, W, K;
  cin >> H >> W >> K;
  int px = -1, py = -1;
  for (auto i = (1); i < H + 1; i++) {
    cin >> ar[i];
    ar[i] = "#" + ar[i];
    for (auto j = (1); j < W + 1; j++) {
      if (ar[i][j] == 'S') {
        px = i;
        py = j;
      }
    }
  }
  queue<pair<int, int> > q;
  q.push({px, py});
  map<pair<int, int>, int> dis, vis;
  dis[{px, py}] = 0;
  int ans = 1000000007;
  vector<pair<int, int> > moves = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  while (!q.empty()) {
    pair<int, int> a = q.front();
    ;
    vis[a] = 1;
    q.pop();
    ;
    ans = min(ans, (min(min(a.first - 1, -a.first + H),
                        min(a.second - 1, -a.second + W)) +
                    K - 1) /
                           (K) +
                       1);
    ;
    if (dis[{a.first, a.second}] == K || vis[{a.first, a.second}]) {
      continue;
    }
    for (auto x : moves) {
      pair<int, int> y = {a.first + x.first, a.second + x.second};
      if (y.first > H || y.first < 0 || y.second > W || y.second < 0) continue;
      if (ar[y.first][y.second] == '#') continue;
      if (!vis[y]) {
        q.push({y.first, y.second});
        if (dis[{y.first, y.second}] != 0)
          dis[{y.first, y.second}] =
              min(dis[{a.first, a.second}] + 1, dis[{y.first, y.second}]);
        else
          dis[{y.first, y.second}] = dis[{a.first, a.second}] + 1;
      }
    }
  }
  cout << ans << endl;
}
