#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
  o << "(" << p.first << "," << p.second << ")";
  return o;
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> vec(h);
  for (int i = (int)(0); i < (int)(h); i++) cin >> vec[i];
  vector<vector<int>> memo(h, vector<int>(w, 2147483600));
  queue<pair<int, int>> q;
  for (int i = (int)(0); i < (int)(h); i++)
    for (int j = (int)(0); j < (int)(w); j++)
      if (vec[i][j] == 'S') {
        q.push(make_pair((i), (j)));
        memo[i][j] = 0;
      }
  int ans = 2147483600;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    int d = min(min(x, h - x - 1), min(y, w - y - 1));
    int tmp = (d + k - 1) / k + 1;
    ans = min(ans, tmp);
    if (memo[x][y] > k) continue;
    int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
    for (int i = (int)(0); i < (int)(4); i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w ||
          memo[nx][ny] != 2147483600 || vec[nx][ny] == '#')
        continue;
      memo[nx][ny] = memo[x][y] + 1;
      q.push(make_pair((nx), (ny)));
    }
  }
  cout << ans << endl;
  return 0;
}
