#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<string> v(n);
  for (int i = 0; i < n; ++i) cin >> v[i];
  vector<vector<int> > d(n, vector<int>(m, -1));
  vector<pair<int, int> > q;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (v[i][j] == 'S') {
        q.push_back(make_pair(i, j));
        d[i][j] = 0;
        v[i][j] = '.';
      }
  for (int i = 0; i < q.size(); ++i) {
    int x = q[i].first;
    int y = q[i].second;
    if (x == 0 || x == n - 1 || y == 0 || y == m - 1) {
      cout << 1 << endl;
      return 0;
    }
    for (int dx = -1; dx <= 1; ++dx)
      for (int dy = -1; dy <= 1; ++dy)
        if (dx * dx + dy * dy == 1) {
          int xx = x + dx;
          int yy = y + dy;
          if (d[xx][yy] != -1) continue;
          if (v[xx][yy] != '.') continue;
          d[xx][yy] = d[x][y] + 1;
          q.push_back(make_pair(xx, yy));
        }
  }
  int result = 1000;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (d[i][j] > k || d[i][j] == -1) continue;
      int dist = min(min(i, j), min(n - 1 - i, m - 1 - j));
      int cur = 1 + dist / k + (dist % k ? 1 : 0);
      result = min(result, cur);
    }
  cout << result << endl;
  return 0;
}
