#include <bits/stdc++.h>
using namespace std;
char b[1005][1005];
int d[1005][1005];
int n, m, k;
bool vaild(int i, int j) {
  return i >= 0 && i < n && j >= 0 && j < m && b[i][j] != '#';
}
int asd(int i, int j) {
  int r = i;
  r = min(r, j);
  r = min(r, n - 1 - i);
  r = min(r, n - 1 - j);
  return r;
}
queue<pair<int, int> > q;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  int xs = -1, ys = -1;
  for (int i = 0; i < n; i++) {
    scanf("%s", b[i]);
    for (int j = 0; j < m; j++)
      if (b[i][j] == 'S') xs = i, ys = j;
  }
  memset(d, -1, sizeof(d));
  d[xs][ys] = 0;
  q.push(make_pair(xs, ys));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = -1; i < 2; i++)
      for (int j = -1; j < 2; j++) {
        if (abs(i) + abs(j) == 1) {
          int xx = x + i, yy = y + j;
          if (vaild(xx, yy) && d[xx][yy] < 0) {
            d[xx][yy] = d[x][y] + 1;
            q.push(make_pair(xx, yy));
          }
        }
      }
  }
  int r = 1 << 20;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (d[i][j] >= 0 && d[i][j] <= k) {
        r = min(r, 1 + (asd(i, j) + k - 1) / k);
      }
  printf("%d\n", r);
  return 0;
}
