#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 3;
char c[805][805];
int a[805][805];
int n, m, k;
int sx, sy;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int cnt[805];
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'S') sx = i, sy = j;
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) a[i][j] = INF;
  for (int i = 0; i < 805; i++) cnt[i] = k;
  queue<pair<int, int> > que;
  que.push(make_pair(sx, sy));
  a[sx][sy] = 0;
  while (que.size()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();
    for (int t = 0; t < k; t++) {
      int nx = x, ny = y;
      for (int i = 0; i < 4; i++) {
        nx += dx[i];
        ny += dy[i];
        if (nx < 0 || ny < 0 || nx >= n || ny >= m || a[nx][ny] <= a[x][y] + 1)
          continue;
        if (c[nx][ny] == '.') {
          a[nx][ny] = a[x][y] + 1;
          que.push(make_pair(nx, ny));
        } else {
          a[nx][ny] = a[x][y] + 1;
          if (cnt[a[nx][ny]] <= 0) continue;
          que.push(make_pair(nx, ny));
          c[nx][ny] = '.';
          cnt[a[nx][ny]]--;
        }
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (i == 0 || i == n - 1 || j == 0 || j == m - 1) ans = min(ans, a[i][j]);
  cout << ans;
  return 0;
}
