#include <bits/stdc++.h>
using namespace std;
int n, m, k;
queue<pair<int, int> > q;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char a[801][801];
int d[801][801];
int sti, stj;
bool check(int x, int y) {
  if (x < 1 || x > n) return 0;
  if (y < 1 || y > m) return 0;
  if (a[x][y] != '.') return 0;
  return 1;
}
int get(int x) {
  int y = x / k + 1;
  if (x % k == 0) y--;
  return y;
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      d[i][j] = 1e9 + 7;
      if (a[i][j] == 'S') {
        sti = i;
        stj = j;
      }
    }
  }
  d[sti][stj] = 0;
  q.push(make_pair(sti, stj));
  while (q.size()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tox = x + dx[i];
      int toy = y + dy[i];
      if (!check(tox, toy)) continue;
      if (d[x][y] + 1 < d[tox][toy]) {
        q.push(make_pair(tox, toy));
        d[tox][toy] = d[x][y] + 1;
      }
    }
  }
  int ans = 1e9 + 7;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (d[i][j] > k) continue;
      int cur = min({m - j, m - 1, n - i, i - 1});
      if (cur == 0) {
        cout << 1;
        return 0;
      }
      ans = min(ans, get(cur));
    }
  }
  cout << ans + 1;
  return 0;
}
