#include <bits/stdc++.h>
const int maxn = 8e2 + 10;
using namespace std;
char a[maxn][maxn];
int stmp[maxn][maxn];
int direction[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int n, m, k;
int ans = 99999999;
bool check(int x, int y) {
  if (x >= 0 && x < n && y >= 0 && y < m && stmp[x][y] == 0 && a[x][y] == '.') {
    return 1;
  }
  return 0;
}
queue<pair<int, int> > po;
int main() {
  cin >> n >> m >> k;
  int sx, sy;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      printf("%c", a[i][j]);
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
    getchar();
  }
  stmp[sx][sy] = 1;
  po.push(make_pair(sx, sy));
  while (!po.empty()) {
    int x = po.front().first;
    int y = po.front().second;
    ans = min(ans, min(min(x, n - x - 1), min(y, m - y - 1)));
    po.pop();
    for (int i = 0; i < 4; i++) {
      int xx = x + direction[i][0];
      int yy = y + direction[i][1];
      if (check(xx, yy) && stmp[x][y] <= k) {
        stmp[xx][yy] = stmp[x][y] + 1;
        po.push(make_pair(xx, yy));
      }
    }
  }
  int anss;
  anss = 1 + (ans + k - 1) / k;
  cout << anss << endl;
  return 0;
}
