#include <bits/stdc++.h>
using namespace std;
char a[810][810];
int ans;
queue<pair<pair<int, int>, int> > q;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
int n, m, k;
bool IN(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
int main() {
  scanf("%d%d%d", &n, &m, &k);
  int x, y;
  for (int i = 0; i < n; i++) {
    scanf("%s", a[i]);
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'S') x = i, y = j;
    }
  }
  q.push(make_pair(make_pair(x, y), k)), a[x][y] = '#';
  int ans = 0x3f3f3f3f;
  while (!q.empty()) {
    pair<pair<int, int>, int> t = q.front();
    q.pop();
    int x = t.first.first, y = t.first.second, cnt = t.second;
    if (t.second == 0) continue;
    ans = min(ans, (min(min(x, y), min(n - x - 1, m - y - 1)) + k - 1) / k);
    for (int i = 0; i < 4; i++) {
      int xx = x + dir[i][0], yy = y + dir[i][1];
      if (!IN(xx, yy) || a[xx][yy] != '.') continue;
      a[xx][yy] = '#';
      q.push(make_pair(make_pair(xx, yy), cnt - 1));
    }
  }
  printf("%d\n", ans + 1);
  return 0;
}
