#include <bits/stdc++.h>
using namespace std;
bool a[805][805], v[805][805];
int b[805][805];
queue<pair<int, int> > q;
int main() {
  int n, m, k, i, j, sx, sy, ans = 2147483647;
  scanf("%d%d%d", &n, &m, &k);
  for (i = 1; i <= n; i++) {
    string c;
    cin >> c;
    for (j = 1; j <= m; j++) {
      if (c[j - 1] == 'S')
        sx = i, sy = j, a[i][j] = true;
      else if (c[j - 1] == '#')
        a[i][j] = false;
      else
        a[i][j] = true;
      b[i][j] = 2147483647;
    }
  }
  int x = sx, y = sy;
  q.push(make_pair(x, y));
  b[x][y] = 0;
  while (!q.empty()) {
    pair<int, int> t = q.front();
    q.pop();
    x = t.first;
    y = t.second;
    if (v[x][y] || !a[x][y]) continue;
    if (x > n || y > m || x < 1 || y < 1) continue;
    v[x][y] = true;
    if (a[x + 1][y])
      b[x + 1][y] = min(b[x + 1][y], b[x][y] + 1), q.push(make_pair(x + 1, y));
    if (a[x - 1][y])
      b[x - 1][y] = min(b[x - 1][y], b[x][y] + 1), q.push(make_pair(x - 1, y));
    if (a[x][y + 1])
      b[x][y + 1] = min(b[x][y + 1], b[x][y] + 1), q.push(make_pair(x, y + 1));
    if (a[x][y - 1])
      b[x][y - 1] = min(b[x][y - 1], b[x][y] + 1), q.push(make_pair(x, y - 1));
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (!a[i][j]) continue;
      int tt = b[i][j];
      if (b[i][j] <= k) tt = k;
      int w = min(min(i - 1, j - 1), min(n - i, m - j));
      tt += w;
      tt = (tt - 1) / k + 1;
      if (tt < ans) ans = tt;
    }
  }
  printf("%d\n", ans);
  return 0;
}
