#include <bits/stdc++.h>
using namespace std;
ifstream in("file.in");
ofstream out("file.out");
long long n, m, v[805][805], ans = 1e9 + 5, first, second, k;
char a[805][805];
queue<pair<long long, long long> > q;
int32_t main() {
  ios_base ::sync_with_stdio(0);
  in >> n >> m >> k;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      in >> a[i][j];
      if (a[i][j] == 'S') first = i, second = j, a[i][j] = '.';
    }
  }
  q.push({first, second});
  v[first][second] = 1;
  while (q.size()) {
    first = q.front().first, second = q.front().second;
    q.pop();
    if (first < n && !v[first + 1][second] && a[first + 1][second] == '.')
      q.push({first + 1, second}), v[first + 1][second] = v[first][second] + 1;
    if (second < m && !v[first][second + 1] && a[first][second + 1] == '.')
      q.push({first, second + 1}), v[first][second + 1] = v[first][second] + 1;
    if (first > 1 && !v[first - 1][second] && a[first - 1][second] == '.')
      q.push({first - 1, second}), v[first - 1][second] = v[first][second] + 1;
    if (second > 1 && !v[first][second - 1] && a[first][second - 1] == '.')
      q.push({first, second - 1}), v[first][second - 1] = v[first][second] + 1;
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if (v[i][j] && v[i][j] <= k + 1) {
        ans = min(ans, (i + k - 2) / k);
        ans = min(ans, (n - i + k - 1) / k);
        ans = min(ans, (j + k - 2) / k);
        ans = min(ans, (m - j + k - 1) / k);
      }
    }
  }
  cout << ans + 1;
}
