#include <bits/stdc++.h>
using namespace std;
const int maxn = 800 + 50;
const int mod = 1e9 + 7;
int adjx[] = {0, 1, 0, -1};
int adjy[] = {1, 0, -1, 0};
string s[maxn];
int h[maxn][maxn];
int k;
bool inrange(int l, int m, int r) { return l <= m and m < r; }
int dis(int a, int b) { return ceil(abs(a - b) / k); }
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) cin >> s[i];
  memset(h, -1, sizeof h);
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'S') q.push({i, j}), h[i][j] = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int z = 0; z < 4; z++) {
      int nx = x + adjx[z], ny = y + adjy[z];
      if (!inrange(0, nx, n) or !inrange(0, ny, m) or h[nx][ny] != -1 or
          s[nx][ny] == '#')
        continue;
      h[nx][ny] = h[x][y] + 1;
      q.push({nx, ny});
    }
  }
  int answer = n + m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (h[i][j] == -1 or h[i][j] > k) continue;
      if (i == 0 or j == 0 or i == n - 1 or j == m - 1)
        return cout << 1 << endl, 0;
      int mx = min(dis(0, i), dis(i, n - 1));
      int my = min(dis(0, j), dis(j, m - 1));
      answer = min(answer, min(mx, my) + 1);
    }
  }
  cout << answer << endl;
}
