#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
bool board[N][N];
int d[N][N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int n, m, k;
  cin >> n >> m >> k;
  pair<int, int> pos;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == 'S') {
        pos = {i, j};
        board[i][j] = true;
      } else if (s[j] == '.') {
        board[i][j] = true;
      }
    }
  }
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      d[i][j] = 1e9;
    }
  }
  q.push(pos);
  d[pos.first][pos.second] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x = p.first;
    int y = p.second;
    if (x > 0 && board[x - 1][y]) {
      if (d[x - 1][y] > d[x][y] + 1) {
        d[x - 1][y] = d[x][y] + 1;
        q.push({x - 1, y});
      }
    }
    if (x < n - 1 && board[x + 1][y]) {
      if (d[x + 1][y] > d[x][y] + 1) {
        d[x + 1][y] = d[x][y] + 1;
        q.push({x + 1, y});
      }
    }
    if (y > 0 && board[x][y - 1]) {
      if (d[x][y - 1] > d[x][y] + 1) {
        d[x][y - 1] = d[x][y] + 1;
        q.push({x, y - 1});
      }
    }
    if (y < m - 1 && board[x][y - 1]) {
      if (d[x][y + 1] > d[x][y] + 1) {
        d[x][y + 1] = d[x][y] + 1;
        q.push({x, y + 1});
      }
    }
  }
  int res = 1e9;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (d[i][j] <= k) {
        int tmp = min(i, n - 1 - i);
        tmp = min(tmp, min(j, m - 1 - j));
        res = min(res, (tmp + k - 1) / k);
      }
    }
  }
  cout << res + 1 << endl;
  return 0;
}
