#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 29;
const long long INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const long long mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int DX[8] = {-1, -1, -1, 0, 1, 1, 1, 0},
          DY[8] = {1, 0, -1, -1, -1, 0, 1, 1};
int h, w, k;
vector<vector<char> > a;
int main() {
  cin >> h >> w >> k;
  a = vector<vector<char> >(h, vector<char>(w));
  int x, y;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  vector<vector<bool> > used(h, vector<bool>(w, 0));
  queue<pair<pair<int, int>, int> > q;
  q.push({{x, y}, 0});
  used[x][y] = 1;
  int res = inf;
  while (!q.empty()) {
    pair<pair<int, int>, int> p = q.front();
    q.pop();
    int X = p.first.first, Y = p.first.second, I = p.second;
    int m = min(min(X, h - X - 1), min(Y, w - Y - 1));
    res = (m + k - 1) / k + 1;
    if (I == k) continue;
    for (int i = 0; i < 4; i++) {
      int cx = X + dx[i], cy = Y + dy[i];
      if (cx >= 0 && cx < h && cy >= 0 && cy < w && a[cx][cy] == '.' &&
          !used[cx][cy]) {
        q.push({{cx, cy}, I + 1});
        used[cx][cy] = 1;
      }
    }
  }
  cout << res << endl;
}
