#include <bits/stdc++.h>
using namespace std;
const int Nmax = 900;
int n, m, k, d[Nmax][Nmax], ans = 1e9 + 10;
char a[Nmax][Nmax];
pair<int, int> s, mv[] = {pair<int, int>(-1, 0), pair<int, int>(1, 0),
                          pair<int, int>(0, -1), pair<int, int>(0, 1)};
queue<pair<int, int> > qu;
void visit() {
  qu.push(s);
  while (!qu.empty()) {
    pair<int, int> u = qu.front();
    qu.pop();
    for (int i = 0; i < 4; ++i) {
      int x = u.first + mv[i].first, y = u.second + mv[i].second;
      if (a[x][y] == '#' || d[x][y] == k) continue;
      if (x == 1 || x == n + 1 || y == 1 || y == m + 1) continue;
      if (d[x][y] == -1) {
        d[x][y] = d[u.first][u.second] + 1;
        qu.push(pair<int, int>(x, y));
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  memset(d, -1, sizeof(d));
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'S') s = pair<int, int>(i, j);
    }
  d[s.first][s.second] = 0;
  visit();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if (d[i][j] == -1) continue;
      int tmp = min(min(i - 1, n - i), min(j - 1, m - j));
      tmp = ceil(float(tmp) / k);
      ans = min(ans, 1 + tmp);
    }
  cout << ans;
}
