#include <bits/stdc++.h>
using namespace std;
char y[809][809];
bool viz[809][809];
bool vizz[809][809];
int main() {
  long long h, w, k, X, Y;
  cin >> h >> w >> k;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      cin >> y[i][j];
      if (y[i][j] == 'S') {
        X = i;
        Y = j;
      }
    }
  }
  stack<pair<long long, long long> > stk;
  stack<pair<long long, long long> > sttk;
  stack<pair<long long, long long> > stttk;
  pair<long long, long long> v;
  stk.push(make_pair(X, Y));
  stttk.push(make_pair(X, Y));
  viz[X][Y] = 1;
  long long ans = 0;
  for (long long i = 0; i < k; i++) {
    while (!stk.empty()) {
      v = stk.top();
      if (v.first > 0) {
        if (y[v.first - 1][v.second] != '#') {
          sttk.push(make_pair(v.first - 1, v.second));
        }
      } else {
        cout << 1;
        return 0;
      }
      if (v.first < h) {
        if (y[v.first + 1][v.second] != '#') {
          sttk.push(make_pair(v.first + 1, v.second));
        }
      } else {
        cout << 1;
        return 0;
      }
      if (v.second > 0) {
        if (y[v.first][v.second - 1] != '#') {
          sttk.push(make_pair(v.first, v.second - 1));
        }
      } else {
        cout << 1;
        return 0;
      }
      if (v.second < w) {
        if (y[v.first][v.second + 1] != '#') {
          sttk.push(make_pair(v.first, v.second + 1));
        }
      } else {
        cout << 1;
        return 0;
      }
      stk.pop();
    }
    while (!sttk.empty()) {
      v = sttk.top();
      if (v.first == 0 || v.second == 0 || v.first == h || v.second == w) {
        cout << 1;
        return 0;
      }
      if (!viz[v.first][v.second]) {
        stk.push(v);
        stttk.push(v);
        viz[v.first][v.second] = 1;
      }
      sttk.pop();
    }
  }
  ans = 1e9;
  while (!stttk.empty()) {
    v = stttk.top();
    ans = min(ans, v.first);
    ans = min(ans, v.second);
    ans = min(ans, w - v.second);
    ans = min(ans, h - v.first);
    stttk.pop();
  }
  if (ans % k == 0) {
    cout << ans / k + 1;
  } else {
    cout << ans / k + 2;
  }
}
