#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& s, vector<T> t) {
  for (int i = 0; i < (int)(t.size()); i++) s << (i ? " " : "") << t[i];
  return s;
}
template <typename T>
istream& operator>>(istream& s, vector<T>& t) {
  for (int i = 0; i < (int)(t.size()); i++) s >> t[i];
  return s;
}
template <typename T, typename U>
ostream& operator<<(ostream& s, pair<T, U> t) {
  s << "(" << t.first << "," << t.second << ")";
  return s;
}
template <typename T, typename U>
istream& operator>>(istream& s, pair<T, U>& t) {
  s >> t.first >> t.second;
  return s;
}
const int N = 888;
bool seen[N][N];
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};
vector<string> s;
int h, w;
bool canGo(int i, int j) {
  if (i < 0 || j < 0 || i >= h || j >= w) return false;
  return s[i][j] != '#';
}
void dfs(int i, int j, int k) {
  if (seen[i][j]) return;
  seen[i][j] = true;
  if (k == 0) return;
  for (int dir = 0; dir < (int)(4); dir++) {
    int ni = i + di[dir];
    int nj = j + dj[dir];
    if (!canGo(ni, nj)) continue;
    dfs(ni, nj, k - 1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int k;
  cin >> h >> w >> k;
  s.resize(h);
  cin >> s;
  int i0 = 0, j0 = 0;
  for (int i = 0; i < (int)(h); i++)
    for (int j = 0; j < (int)(w); j++) {
      if (s[i][j] == 'S') {
        i0 = i;
        j0 = j;
      }
    }
  dfs(i0, j0, k);
  int best = h * w;
  for (int i = 0; i < (int)(h); i++)
    for (int j = 0; j < (int)(w); j++) {
      if (seen[i][j]) {
        int cur = min(min(i, j), min(h - i - 1, w - j - 1));
        if (cur == 0) {
          cout << 1 << endl;
          return 0;
        }
        cur = (cur - 1) / k + 1;
        best = min(best, cur);
      }
    }
  cout << best + 1 << endl;
}
