#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  int d[4] = {0, 1, 0, -1};
  cin >> h >> w >> k;
  string a[800];
  for (int i = 0; i < h; ++i) cin >> a[i];
  vector<vector<int>> dist(h, vector<int>(w, 1000));
  deque<pair<int, int>> que;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (a[i][j] == 'S') {
        que.push_back(pair<int, int>(i, j));
        dist[i][j] = 0;
        break;
      }
  bool f = true;
  while (f) {
    pair<int, int> t = que.front();
    f = false;
    for (int i = 0; i < 4; ++i) {
      int x = t.first + d[i], y = t.second + d[i ^ 1];
      if (x < 0 || x >= h || y < 0 || y >= w) continue;
      if (dist[x][y] && a[x][y] == '.') {
        dist[x][y] = 0;
        que.push_back(pair<int, int>(x, y));
      }
    }
  }
  while (!que.empty()) {
    pair<int, int> t = que.front();
    que.pop_front();
    for (int i = 0; i < 4; ++i) {
      int x = t.first + d[i], y = t.second + d[i ^ 1];
      if (x < 0 || x >= h || y < 0 || y >= w) continue;
      if (dist[x][y] == 1000) {
        dist[x][y] = dist[t.first][t.second] + 1;
        que.push_back(pair<int, int>(x, y));
      }
    }
  }
  int ans = 1000;
  for (int i = 0; i < h; ++i) ans = min(ans, min(dist[i][0], dist[i][w - 1]));
  for (int i = 0; i < w; ++i) ans = min(ans, min(dist[0][i], dist[h - 1][i]));
  cout << (ans + k - 1) / k + 1 << endl;
  return 0;
}
