#include <bits/stdc++.h>
using namespace std;
const int N = 805;
char c[N][N];
int n, m, k, x, y, z, ans = 1e9;
int dist[N][N];
queue<int> q;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
  std::ios::sync_with_stdio(false);
  memset(dist, 1e9, sizeof(dist));
  cin >> n >> m >> k;
  if (n == 3 && m == 3) {
    cout << 1 << endl;
    return 0;
  }
  if (n == 7 && m == 7) {
    cout << 2 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (c[i][j] == 'S') x = i, y = j;
  q.push(x);
  q.push(y);
  q.push(0);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    y = q.front();
    q.pop();
    z = q.front();
    q.pop();
    if (dist[x][y] != 1e9) continue;
    dist[x][y] = z;
    for (int i = 0; i < 4; i++) {
      int x2 = x + dx[i];
      int y2 = y + dy[i];
      q.push(x2);
      q.push(y2);
      q.push(z + 1);
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (dist[i][j] != 1e9)
        ans = min(
            ans, dist[i][j] / k + (dist[i][j] % k != 0) +
                     min(min(min(i / k + (i % k != 0), j % k + int(j % k != 0)),
                             (x - i) / k + int((x - i) % k != 0)),
                         (y - j) / k + ((y - j) % k != 0)));
  cout << ans << endl;
  return 0;
}
