#include <bits/stdc++.h>
using namespace std;
const int MAXN = 810;
vector<pair<int, int> > v;
int go[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
int n, m, k, sx, sy, book[MAXN][MAXN];
char a[MAXN][MAXN];
int count(int x) { return (x + k - 1) / k; }
int make(pair<int, int> p) {
  int m1 = min(count(p.first - 1), count(n - p.first));
  int m2 = min(count(p.second - 1), count(m - p.second));
  return min(m1, m2);
}
void dfs(int x, int y, int step) {
  v.push_back(pair<int, int>(x, y));
  book[x][y] = 1;
  if (step == k) return;
  for (int i = 0; i < 4; i++) {
    int tx = x + go[i][0];
    int ty = y + go[i][1];
    if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
    if (a[tx][ty] == '.' && !book[tx][ty]) dfs(tx, ty, step + 1);
  }
}
void solve() {
  int ans = 0x3f3f3f3f;
  if (sx == 1 || sx == n || sy == 1 || sy == m) {
    printf("0");
    return;
  }
  dfs(sx, sy, 0);
  for (int i = 0; i < v.size(); i++) ans = min(ans, make(v[i]));
  printf("%d", ans + 1);
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      scanf("%c", &a[i][j]);
      if (a[i][j] == 'S') sx = i, sy = j;
    }
  }
  solve();
  return 0;
}
