#include <bits/stdc++.h>
using namespace std;
const int M = 888;
const int N = M * M;
int n, m, k;
char a[M][M];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool u[M][M];
int d[M][M];
pair<int, int> q[N];
int qs = 0, qf = 0;
void read() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) cin >> a[i][j];
}
bool inside(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m; }
void bfs() {
  while (qs < qf) {
    int x, y;
    tie(x, y) = q[qs++];
    for (int i = 0; i < 4; ++i) {
      int tx = x + dx[i];
      int ty = y + dy[i];
      if (inside(tx, ty) && !u[tx][ty] && a[tx][ty] != '#') {
        d[tx][ty] = d[x][y] + 1;
        u[tx][ty] = true;
        q[qf++] = pair<int, int>{tx, ty};
      }
    }
  }
}
int dist(int x, int y) {
  int ans = min({x, n - x, y, m - y});
  return ans;
}
void kill() {
  int sx, sy;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == 'S') sx = i, sy = j;
    }
  u[sx][sy] = true;
  q[qf++] = pair<int, int>{sx, sy};
  bfs();
  int ans = 1e9;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (u[i][j] && d[i][j] <= k) {
        ans = min(ans, 1 + (dist(i, j) + k - 1) / k);
      }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  read();
  kill();
}
