#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  string s[805];
  for (int i = 0; i < h; ++i) cin >> s[i];
  int d[4] = {0, 1, 0, -1};
  vector<vector<bool>> used(h, vector<bool>(w, false));
  queue<pair<int, int>> que;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (s[i][j] == 'S') {
        que.push(make_pair(i, j));
        used[i][j] = true;
      }
  for (int i = 0; i < k; ++i) {
    int t = que.size();
    for (int j = 0; j < t; ++j) {
      pair<int, int> p = que.front();
      for (int l = 0; l < 4; ++l) {
        int x = p.first + d[l], y = p.second + d[l ^ 1];
        if (x < 0 || x >= h || y < 0 || y >= w) continue;
        if (!used[x][y] && s[x][y] == '.') {
          used[x][y] = true;
          que.push(make_pair(x, y));
        }
      }
    }
  }
  int ans = 1000;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (used[i][j])
        ans = min(ans, (min({i, j, h - i - 1, w - j - 1}) + k - 1) / k + 1);
  cout << ans << endl;
  return 0;
}
