#include <bits/stdc++.h>
using namespace std;
const int MAXN = 800 + 10;
int n, m, k;
string s[MAXN];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int lv[MAXN][MAXN];
pair<int, int> q[MAXN * MAXN];
bool fit(int x, int y) {
  return 0 <= x && x < n && 0 <= y && y < m && s[x][y] != '#';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) cin >> s[i];
  pair<int, int> a;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'S') a = {i, j};
  memset(lv, 63, sizeof(lv));
  int h = 0, t = 0;
  q[t++] = a;
  lv[a.first][a.second] = 0;
  while (t - h) {
    auto v = q[h++];
    if (lv[v.first][v.second] > k) break;
    for (int w = 0; w < 4; w++) {
      pair<int, int> u(v.first + dx[w], v.second + dy[w]);
      if (fit(u.first, u.second) &&
          lv[v.first][v.second] + 1 < lv[u.first][u.second]) {
        lv[u.first][u.second] = lv[v.first][v.second] + 1;
        q[t++] = u;
      }
    }
  }
  int ans = 1e9;
  for (int i = 0; i < h; i++) {
    auto v = q[i];
    ans = min(ans, 1 + (v.first + k - 1) / k);
    ans = min(ans, 1 + (v.second + k - 1) / k);
    ans = min(ans, 1 + (n - 1 - v.first + k - 1) / k);
    ans = min(ans, 1 + (m - 1 - v.second + k - 1) / k);
  }
  cout << ans << endl;
  return 0;
}
