#include <bits/stdc++.h>
using namespace std;
char grid[801][801];
bitset<801> vis[801];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  queue<vector<int>> q;
  int best = 1e9;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> grid[i][j];
      if (grid[i][j] == 'S') q.push({i, j, 0});
    }
  }
  while (!q.empty()) {
    vector<int> state = q.front();
    q.pop();
    int x = state[0], y = state[1], moves = state[2];
    int mini = min(x, min(y, min(h - x - 1, w - y - 1)));
    best = min(best, 1 + mini / k + (mini % k != 0));
    if (moves == k) break;
    for (int dx = -1; dx <= 1; ++dx) {
      for (int dy = -1; dy <= 1; ++dy) {
        if (dx == 0 && dy == 0) continue;
        if (dx != 0 && dy != 0) continue;
        if (x + dx < 0 || x + dx == h) continue;
        if (y + dy < 0 || y + dy == w) continue;
        if (grid[x + dx][y + dy] == '#') continue;
        if (vis[x + dx][y + dy]) continue;
        vis[x + dx][y + dy] = 1;
        q.push({x + dx, y + dy, moves + 1});
      }
    }
  }
  cout << best << "\n";
  return 0;
}
