#include <bits/stdc++.h>
using namespace std;
int n, m;
const int nmax = 800;
const int inf = 1 << 30;
const int dl[4] = {-1, 0, 0, 1};
const int dc[4] = {0, -1, 1, 0};
int viz[nmax + 1][nmax + 1];
queue<pair<int, int>> q;
string s[nmax + 1];
inline int mndst(int x, int y) {
  int dist = min(x - 1, y - 1);
  return min(dist, min(n - x, m - y));
}
int main() {
  int k;
  cin >> n >> m >> k;
  int x, y;
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    for (int j = 1; j <= m; ++j) {
      if (s[i][j - 1] == 'S') {
        x = i, y = j;
      }
    }
    s[i] = "#" + s[i];
  }
  viz[x][y] = 1;
  q.push(make_pair(x, y));
  int ans = inf;
  while (!q.empty()) {
    pair<int, int> shp = q.front();
    q.pop();
    ans = min(ans, mndst(shp.first, shp.second));
    if (viz[shp.first][shp.second] == k) continue;
    for (int dir = 0; dir < 4; ++dir) {
      int a, b;
      a = shp.first + dl[dir], b = shp.second + dc[dir];
      if (a < 1 || b < 1 || a > n || b > m) continue;
      if (viz[a][b] != 0 || s[a][b] == '#') continue;
      viz[a][b] = viz[shp.first][shp.second] + 1;
      q.push(make_pair(a, b));
    }
  }
  if (ans <= 0)
    ans = 0;
  else
    ans = (ans - 1) / k + 1;
  if (x == 1 || y == 1 || x == n || y == m) ans = -1;
  cout << ans + 1;
  return 0;
}
