#include <bits/stdc++.h>
using namespace std;
const int N = 800 + 5;
const int inf = 1e9 + 20;
int n, m, k;
char a[N][N];
int sx, sy;
int ans = inf;
int d[N][N];
queue<pair<int, int> > q;
int dx[] = {-1, 0, +1, 0};
int dy[] = {0, -1, 0, +1};
void bfs(int sx, int sy) {
  d[sx][sy] = 0;
  q.push({sx, sy});
  while (q.size()) {
    int vx = q.front().first;
    int vy = q.front().second;
    q.pop();
    if (vx == 0 || vx == n - 1 || vy == 0 || vy == m - 1) {
      cout << 1;
      exit(0);
    }
    int res = min({vx, n - 1 - vx, vy, m - 1 - vy});
    ans = min(ans, 1 + (res + k - 1) / k);
    if (d[vx][vy] == k) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      int ux = vx + dx[i];
      int uy = vy + dy[i];
      if (a[ux][uy] != '#' && d[vx][vy] + 1 < d[ux][uy]) {
        d[ux][uy] = d[vx][vy] + 1;
        q.push({ux, uy});
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  bfs(sx, sy);
  cout << ans << endl;
}
