#include <bits/stdc++.h>
using namespace std;
const int maxn = 2333;
struct pii {
  int x, y, dep;
  pii(int _x, int _y, int _d) { x = _x, y = _y, dep = _d; }
};
int dirx[] = {0, 0, 1, -1};
int diry[] = {1, -1, 0, 0};
int h, w, k;
char xxx[maxn][maxn];
bool vis[maxn][maxn];
int anss[maxn];
inline int get(int x, int y) { return min(min(x, h - x), min(y, w - y)); }
int main() {
  scanf("%d%d%d", &h, &w, &k);
  queue<pii> q;
  int ans = 2333333;
  for (int i = 1; i <= h; ++i) {
    scanf("%s", xxx[i] + 1);
    for (int j = 1; j <= w; ++j) {
      if (xxx[i][j] == 'S') {
        q.push(pii(i, j, 0));
        vis[i][j] = true;
      }
    }
  }
  while (!q.empty()) {
    pii now = q.front();
    q.pop();
    int too = get(now.x, now.y);
    int tmpans = max(1, int(ceil((double)now.dep / k)));
    int oth = ceil((double)too / k);
    ans = min(ans, tmpans + oth);
    for (int i = 0; i < 4; ++i) {
      int xx = now.x + dirx[i];
      int yy = now.y + diry[i];
      if (xx && yy && xx <= h && yy <= w && !vis[xx][yy] &&
          xxx[xx][yy] == '.') {
        vis[xx][yy] = true;
        q.push(pii(xx, yy, now.dep + 1));
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
