#include <bits/stdc++.h>
using namespace std;
const unsigned long long MOD = 1000000007LL;
long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); }
long long fast_exp(long long base, long long exp) {
  long long tot = 1;
  for (; exp > 0; exp >>= 1) {
    if ((exp & 1) == 1) tot = tot * base % MOD;
    base = base * base % MOD;
  }
  return tot;
}
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<pair<int, 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(make_pair(i, j));
      }
    }
  }
  while (!q.empty()) {
    pair<int, int> state = q.front();
    q.pop();
    int x = state.first, y = state.second;
    cout << "VIS " << x << " " << y << endl;
    int mini = min(x, min(y, min(h - x - 1, w - y - 1)));
    best = min(best, 1 + mini / k + (mini % k > 0));
    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(make_pair(x + dx, y + dy));
      }
    }
  }
  cout << best << "\n";
  return 0;
}
