#include <bits/stdc++.h>
using namespace std;
int vis[800][800];
char a[800][800];
int h, w, k;
int u, d, l, r;
bool check(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w && a[x][y] != '#' && !vis[x][y])
    return true;
  return false;
}
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
void dfs(int x, int y, int k) {
  u = min(x, u);
  d = max(x, d);
  l = min(l, y);
  r = max(r, y);
  vis[x][y] = 1;
  if (k == 0) return;
  for (int i = 0; i < 4; i++) {
    if (check(x + dx[i], y + dy[i])) {
      dfs(x + dx[i], y + dy[i], k - 1);
    }
  }
}
int main(void) {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        u = d = i;
        l = r = j;
      }
    }
  }
  dfs(u, l, k);
  int dmin = min(u - 0, min(h - 1 - d, min(l - 0, w - 1 - r)));
  cout << dmin / k + (dmin % k != 0) + 1 << endl;
  return 0;
}
