#include <bits/stdc++.h>
using namespace std;
const int maxn = 801;
int n, m, x, y, ind[maxn], k, b[maxn][maxn];
char s[maxn][maxn];
void dfs(int x, int y, int k) {
  if (b[x][y] || x < 0 || x > n || y < 0 || y > m || k < 0 || s[x][y] == '#')
    return;
  b[x][y] = 1;
  dfs(x + 1, y, k - 1);
  dfs(x - 1, y, k - 1);
  dfs(x, y + 1, k - 1);
  dfs(x, y - 1, k - 1);
}
signed main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) cin >> (s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'S') x = i, y = j;
  dfs(x, y, k);
  int ans = n + m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int x = min(i, min(n - i + 1, min(j, m - j + 1)));
      x--;
      if (b[i][j]) {
        ans = min(ans, (int)ceil((double)x / k + 1));
      }
    }
  cout << ans;
}
