#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long double EPS = 1e-6;
int n, m, k;
const int N = 810;
char c[N][N];
bool vis[N][N];
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
void dfs(int x, int y, int s, int k) {
  vis[x][y] = true;
  if (s == k) return;
  for (int(i) = 0; (i) < (int)4; (i)++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (c[nx][ny] != '#' && !vis[nx][ny]) dfs(nx, ny, s + 1, k);
  }
}
int calc(int x, int y) { return min(x - 1, min(y - 1, min(n - x, m - y))); }
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
  int stx = -1, sty = -1;
  for (int(i) = 1; (i) <= (int)n; (i)++)
    for (int(j) = 1; (j) <= (int)m; (j)++)
      if (c[i][j] == 'S') {
        stx = i;
        sty = j;
        break;
      }
  dfs(stx, sty, 0, k);
  int ans = INF;
  for (int(i) = 1; (i) <= (int)n; (i)++)
    for (int(j) = 1; (j) <= (int)m; (j)++)
      if (vis[i][j]) ans = min(ans, calc(i, j));
  cout << ceil(ans / double(k)) + 1 << endl;
  return 0;
}
