#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
char s[N][N];
int n, m, k;
int sx, sy;
int z[N][N];
int ans = 1 << 30;
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
queue<pair<int, int> > q;
bool vis[N][N];
int deep[N][N];
void bfs() {
  q.push(pair<int, int>(sx, sy));
  vis[sx][sy] = 1;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    ans = min(ans, z[x][y]);
    if (deep[x][y] == k) continue;
    for (int i = 0; i < 4; i++) {
      int xx = x + dir[i][0], yy = y + dir[i][1];
      if (xx <= 0 || yy <= 0 || xx > n || yy > m || vis[xx][yy]) continue;
      vis[xx][yy] = 1;
      deep[xx][yy] = deep[x][y] + 1;
      q.push(pair<int, int>(xx, yy));
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == 'S') sx = i, sy = j;
      z[i][j] = min(min(i - 1, j - 1), min(n - i, m - j));
      z[i][j] = (z[i][j] + k - 1) / k;
    }
  bfs();
  printf("%d\n", ans + 1);
}
