#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char c[805][805];
int a[805][805];
int d[805][805];
int f[805][805];
int sx, sy;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int INF = 2e9 + 3;
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'S') sx = i, sy = j;
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) f[i][j] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) d[i][j] = abs(sx - i) + abs(sy - j);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) a[i][j] = INF;
  a[sx][sy] = 0;
  queue<pair<int, int> > que;
  que.push(make_pair(sx, sy));
  while (que.size()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
      if (c[nx][ny] != '#') {
        int p = (d[nx][ny] + k - 1) / k;
        if (a[nx][ny] > p) {
          que.push(make_pair(nx, ny));
          a[nx][ny] = p;
        }
      } else {
        int p = (a[nx][ny] + k - 1) / k;
        if (!f[x][y]) {
          p++;
          f[nx][ny] = k;
          f[nx][ny]--;
          d[nx][ny] -= d[nx][ny] % k;
        } else
          f[nx][ny] = f[x][y]--;
        if (p < a[nx][ny]) {
          a[nx][ny] = p;
          que.push(make_pair(nx, ny));
        }
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (i == 0 || i == n - 1 || j == 0 || j == m - 1) ans = min(ans, a[i][j]);
  cout << ans;
  return 0;
}
