#include <bits/stdc++.h>
using namespace std;
int n, m, k;
bool vis[880][880];
char c[880][880];
int sx, sy, ans = 1e9;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
void dfs(int t, int x, int y) {
  vis[x][y] = 1;
  if (t == n) return;
  ans = min(ans, min(min(x, n - x - 1), min(y, m - y - 1)));
  for (int i = 0; i < 4; i++) {
    int u = x + dx[i], v = y + dy[i];
    if (u >= 0 && v >= 0 && u < n && v <= m && !vis[u][v] && c[u][v] != '#') {
      vis[u][v] = 1;
      dfs(t + 1, u, v);
    }
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    for (int j = 0; j < m; j++)
      if (c[i][j] == 'S') {
        sx = i;
        sy = j;
      }
  }
  dfs(0, sx, sy);
  cout << (ans + k - 1) / k + 1 << endl;
  return 0;
}
