#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
const int INF = 1 << 30;
const long long LLINF = 1ll << 60;
const int MOD = 1e9 + 7;
const int xx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
const int yy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
char input[1005][1005];
struct node {
  int x, y;
};
int n, m, k;
int dist[1005][1005];
int bfs(int x, int y) {
  queue<node> q;
  q.push({x, y});
  dist[x][y] = 0;
  while (!q.empty()) {
    node temp = q.front();
    q.pop();
    int tempx = temp.x;
    int tempy = temp.y;
    for (int i = 0; i < 4; i++) {
      int tx = tempx + xx[i];
      int ty = tempy + yy[i];
      if (tx < 0 || tx > n || ty < 0 || ty > m || input[tx][ty] == '#')
        continue;
      else if (dist[tempx][tempy] + 1 < dist[tx][ty]) {
        q.push({tx, ty});
        dist[tx][ty] = dist[tempx][tempy] + 1;
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dist[i][j] != INF && dist[i][j] <= k) {
        ans = min(ans, min(i, min(n - i - 1, min(j, m - j - 1))) / k);
      }
    }
  }
  if (ans == 0 || ans % k != 0 || k == 1) ans++;
  return ans;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  getchar();
  for (int i = 0; i < n; i++) {
    scanf("%s", input[i]);
    for (int j = 0; j < m; j++) {
      dist[i][j] = INF;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (input[i][j] == 'S') {
        ans = bfs(i, j);
        break;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
