#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
mt19937 rnd(chrono::steady_clock().now().time_since_epoch().count());
const int N = 805;
int dis[N][N];
int dr[4][2] = {{-1, 0}, {+1, 0}, {0, +1}, {0, -1}};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) dis[i][j] = (int)-1;
  queue<pair<int, int> > ff;
  char t;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> t;
      if (t == 'S') {
        ff.push(make_pair(i, j));
        dis[i][j] = 0;
      } else if (t == '.') {
        dis[i][j] = (int)1e7;
      } else {
        dis[i][j] = -1;
      }
    }
  }
  int vi, vj;
  while (!ff.empty()) {
    vi = ff.front().first;
    vj = ff.front().second;
    ff.pop();
    if (dis[vi][vj] == k) continue;
    for (int i = 0; i < 4; i++) {
      if (dis[vi + dr[i][0]][vj + dr[i][1]] > dis[vi][vj] + 1) {
        dis[vi + dr[i][0]][vj + dr[i][1]] = dis[vi][vj] + 1;
        ff.push(make_pair(vi + dr[i][0], vj + dr[i][1]));
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dis[i][j] == -1) {
        dis[i][j] = (int)1e7;
      } else if (dis[i][j] <= k) {
        dis[i][j] = k;
        ff.push(make_pair(i, j));
      }
    }
  }
  while (!ff.empty()) {
    vi = ff.front().first;
    vj = ff.front().second;
    ff.pop();
    for (int i = 0; i < 4; i++) {
      if (dis[vi + dr[i][0]][vj + dr[i][1]] > dis[vi][vj] + 1) {
        dis[vi + dr[i][0]][vj + dr[i][1]] = dis[vi][vj] + 1;
        ff.push(make_pair(vi + dr[i][0], vj + dr[i][1]));
      }
    }
  }
  int res = n * m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i == 1 || j == 1 || i == n || j == m) {
        if (dis[i][j] != -1) res = min(res, dis[i][j]);
      }
    }
  }
  cout << (res + k - 1) / k << "\n";
  return 0;
}
