#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
struct Data {
  int x, y, t;
};
const int con[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
bool vis[810][810];
void solve(int h, int w, long long k, vector<string> a) {
  memset(vis, true, sizeof(vis));
  int x = 0, y = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (a[i][j] == 'S') {
        x = i;
        y = j;
        a[i][j] = '.';
        break;
      }
    }
  }
  queue<Data> q;
  q.push({x + 1, y + 1, 0});
  vis[x][y] = false;
  int mn = INT_MAX;
  while (!q.empty()) {
    Data now = q.front();
    q.pop();
    int t = min(now.x - 1, min(now.y - 1, min(h - now.x, w - now.y)));
    mn = min(mn, t);
    if (now.t >= k) continue;
    int x = now.x, y = now.y;
    for (int i = 0; i < 4; ++i) {
      int nx = x + con[i][0], ny = y + con[i][1];
      if (nx >= 1 && nx <= h && ny >= 1 && ny <= w && vis[nx][ny] &&
          a[nx - 1][ny - 1] == '.') {
        vis[nx][ny] = false;
        q.push({nx, ny, now.t + 1});
      }
    }
  }
  cout << int(mn / double(k) + 0.999999) + 1 << endl;
}
int main() {
  long long H;
  long long K;
  long long W;
  scanf("%lld", &H);
  vector<string> A(H - 1 + 1);
  scanf("%lld", &W);
  scanf("%lld", &K);
  for (int i = 0; i <= H - 1; i++) {
    cin >> A[i];
  }
  solve(H, W, K, A);
  return 0;
}
