#include <bits/stdc++.h>
using namespace std;
int n, m, k, d[1116][1116];
char a[1116][1116];
queue<pair<int, int> > q;
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -f;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return x * f;
}
int main() {
  n = read();
  m = read();
  k = read();
  memset(d, 0x3f, sizeof(d));
  for (int i = 1; i <= n; i++) {
    scanf(" %s", a[i] + 1);
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 'S') d[i][j] = 0, q.push(make_pair(i, j));
  }
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = x + dx[i], ty = y + dy[i];
      if (a[tx][ty] == '.' && d[tx][ty] == 0x3f3f3f3f)
        d[tx][ty] = d[x][y] + 1, q.push(make_pair(tx, ty));
    }
  }
  int ans = 0x3fffffff;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (d[i][j] <= k)
        ans = min(ans,
                  1 + (min(min(i - 1, n - i), min(j - 1, m - j)) + k - 1) / k);
  printf("%d\n", ans);
  return 0;
}
