#include <bits/stdc++.h>
using namespace std;
const int N = (int)(1e3 + 5);
const int LOG = (int)(20);
struct node {
  int x, y, stp;
};
int n, m, k, stx, sty, w[4][2] = {1, 0, -1, 0, 0, -1, 0, 1}, ans = INT_MAX,
                       mark[N][N];
string ar[N];
queue<node> q;
void bfs() {
  q.push({stx, sty, 0});
  while (!q.empty()) {
    node t = q.front();
    q.pop();
    mark[t.x][t.y] = 1;
    if (t.stp == k) continue;
    for (int i = 0; i < 4; i++) {
      int nx = t.x + w[i][0], ny = t.y + w[i][1];
      if (nx > 0 && ny > 0 && nx <= n && ny <= m && ar[nx][ny] != '#' &&
          mark[nx][ny] == 0)
        q.push({nx, ny, t.stp + 1});
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
    ar[i] = '#' + ar[i];
  }
  assert(0);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (ar[i][j] == 'S') stx = i, sty = j;
  bfs();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (mark[i][j] == 1) {
        double t1 = abs(n - i), t2 = abs(i - 1), t3 = abs(m - j),
               t4 = abs(j - 1);
        ans = min(ans, (int)ceil(min(min(t1, t2), min(t3, t4)) / k));
      }
  cout << ans + 1;
}
