#include <bits/stdc++.h>
using namespace std;
char c[850][850];
int h, w, k;
int vis[850][850];
int ax, ay;
int xx[] = {0, 0, 1, -1};
int yy[] = {1, -1, 0, 0};
void bfs() {
  for (int i = 0; i < 820; i++)
    for (int j = 0; j < 820; j++) vis[i][j] = 123465;
  queue<pair<int, int> > q;
  q.push(make_pair(ax, ay));
  vis[ax][ay] = 0;
  while (!q.empty()) {
    pair<int, int> o = q.front();
    int ox = o.first, oy = o.second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ix = ox + xx[i], iy = oy + yy[i];
      if (ix > 0 and ix < h + 1 and iy > 0 and iy < w + 1 and
          c[ix][iy] == '.' and vis[ix][iy] > vis[ox][oy] + 1) {
        if (1) q.push(make_pair(ix, iy));
        vis[ix][iy] = vis[ox][oy] + 1;
      }
    }
  }
}
void solve() {
  bfs();
  int ans = 146789;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (vis[i][j] <= k) {
        int ret = min(i - 1, j - 1);
        ret = min(ret, (h - i));
        ret = min(w - j, ret);
        ans = min(ans, ret);
      }
    }
  }
  if (ans % k == 0)
    cout << ans / k + 1;
  else
    cout << ans / k + 2;
}
int main() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'S') ax = i, ay = j;
    }
  }
  solve();
  return 0;
}
