#include <bits/stdc++.h>
using namespace std;
const int DIM = 805;
const int dx[5] = {0, -1, 0, 1, 0};
const int dy[5] = {0, 0, 1, 0, -1};
int dst[DIM][DIM];
char arr[DIM][DIM];
deque<pair<int, int>> que;
inline int f(int x, int k) {
  if (x % k == 0)
    return x / k;
  else
    return x / k + 1;
}
int main(void) {
  int n, m, k;
  cin >> n >> m >> k;
  pair<int, int> ip;
  for (int i = 1; i <= n; i++) {
    cin >> (arr[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (arr[i][j] == 'S') ip = make_pair(i, j);
    }
  }
  dst[ip.first][ip.second] = 1;
  for (que.push_back(ip); que.empty() == false; que.pop_front()) {
    pair<int, int> px = que.front();
    for (int d = 1; d <= 4; d++) {
      pair<int, int> py = make_pair(px.first + dx[d], px.second + dy[d]);
      if (dst[py.first][py.second] == 0 && arr[py.first][py.second] == '.') {
        dst[py.first][py.second] = dst[px.first][px.second] + 1;
        que.push_back(py);
      }
    }
  }
  int ans = numeric_limits<int>::max();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (dst[i][j] == 0 || dst[i][j] > k) continue;
      if (i == 1 || i == n || j == 1 || j == m)
        ans = 0;
      else
        ans = min(ans, min(min(f(i - 1, k), f(n - i, k)),
                           min(f(j - 1, k), f(m - j, k))));
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
