#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int mxn = 800, oo = 1e9;
int n, m, k;
char board[mxn][mxn];
bool vis[mxn][mxn];
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> board[i][j];
  int fi = 0, fj = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (board[i][j] == 'S') fi = i, fj = j;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) vis[i][j] = 0;
  queue<pii> pq;
  pq.push(pii(fi, fj));
  vis[fi][fj] = 1;
  for (int _ = 0; _ < k; _++) {
    queue<pii> nxt;
    while (pq.size()) {
      pii pp = pq.front();
      pq.pop();
      int i = pp.first, j = pp.second;
      for (int t = 0; t < 4; t++) {
        int ni = i + di[t], nj = j + dj[t];
        if ((0 <= ni && ni <= n - 1) && (0 <= nj && nj <= m - 1) &&
            board[ni][nj] != '#' && !vis[ni][nj]) {
          vis[ni][nj] = 1;
          pq.push({ni, nj});
        }
      }
    }
    pq = nxt;
  }
  int ans = oo;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (vis[i][j]) {
        int dx = min(i, n - 1 - i), dy = min(j, m - 1 - j);
        ans = min(ans, 1 + int(min(ceil(1.00 * dx / k), ceil(1.00 * dy / k))));
      }
  cout << ans << endl;
  return 0;
}
