#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
char a[808][808];
int h, w, k;
int dis[808][808];
queue<pair<int, int> > Q;
int main() {
  cin >> h >> w >> k;
  memset(dis, -1, sizeof(dis));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        Q.push(make_pair(i, j));
        dis[i][j] = 0;
      }
    }
  while (!Q.empty()) {
    pair<int, int> x = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = x.first + dx[i], yy = x.second + dy[i];
      if (xx < 0 || yy < 0 || xx >= h || yy >= w) continue;
      if (a[xx][yy] == '.' && dis[xx][yy] == -1) {
        Q.push(make_pair(xx, yy));
        dis[xx][yy] = dis[x.first][x.second] + 1;
      }
    }
  }
  int ans = 1e9 + 7;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (dis[i][j] != -1) {
        int mn = min(min(i, j), min(h - i - 1, w - j - 1));
        ans = min(ans, (mn + k - 1) / k + 1);
      }
  cout << ans << endl;
  return 0;
}
