#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n, m, k, sx, sy, ans;
char c[805][805];
bool vis[805][805];
int mx[] = {0, 0, 1, 0, -1};
int my[] = {0, 1, 0, -1, 0};
inline void dfs(int x, int y) {
  vis[x][y] = true;
  for (int i = 1; i <= 4; i++) {
    int tx = x + mx[i];
    int ty = y + my[i];
    if (tx < 1 || tx > n || ty < 1 || ty > m || vis[tx][ty] || c[tx][ty] != '.')
      continue;
    dfs(tx, ty);
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) scanf("%s", c[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == 'S') {
        sx = i;
        sy = j;
        break;
      }
  dfs(sx, sy);
  ans = INF;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (vis[i][j]) {
        int res = min(i - 1, n - i);
        res = min(res, min(j - 1, m - j));
        ans = min(ans, res);
      }
  cout << 1 + (ans + k - 1) / k;
  return 0;
}
