#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 3;
char c[805][805];
int n, m, k;
int a[805][805];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int sx, sy;
bool ok(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }
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++) a[i][j] = INF;
  a[sx][sy] = 0;
  queue<pair<int, int> > q;
  q.push(make_pair(sx, sy));
  while (q.size()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (ok(nx, ny) && a[nx][ny] > a[x][y] + 1 && c[nx][ny] != '#') {
        a[nx][ny] = a[x][y] + 1;
        q.push(make_pair(nx, ny));
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] <= k) {
        int mn = min(min(i, j), min(n - i - 1, m - j - i));
        int tmp = (k + mn - 1) / k;
        ans = min(ans, tmp + 1);
      }
  cout << ans;
  return 0;
}
