#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = 1 << 30;
int dist[808][808];
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  for (int i = (0); i < (h); ++i) cin >> a[i];
  int sx, sy;
  for (int i = (0); i < (h); ++i)
    for (int j = (0); j < (w); ++j) {
      if (a[i][j] == 'S') {
        sx = j, sy = i;
      }
      dist[i][j] = INF;
    }
  dist[sy][sx] = 0;
  queue<pair<int, int>> q;
  q.emplace(sy, sx);
  while (q.size()) {
    int y = q.front().first;
    int x = q.front().second;
    q.pop();
    for (int i = (-1); i < (2); ++i)
      for (int j = (-1); j < (2); ++j) {
        int ny = y + i;
        int nx = x + j;
        if (0 <= ny && ny < h && 0 <= nx && nx < w && dist[ny][nx] == INF &&
            a[ny][nx] == '.') {
          dist[ny][nx] = dist[y][x] + 1;
          q.emplace(ny, nx);
        }
      }
  }
  for (int i = (0); i < (h); ++i)
    for (int j = (0); j < (w); ++j) {
      if (dist[i][j] <= k) dist[i][j] = 0;
      if (dist[i][j] == 0) q.emplace(i, j);
    }
  while (q.size()) {
    int y = q.front().first;
    int x = q.front().second;
    q.pop();
    for (int i = (-1); i < (2); ++i)
      for (int j = (-1); j < (2); ++j) {
        int ny = y + i;
        int nx = x + j;
        if (0 <= ny && ny < h && 0 <= nx && nx < w && dist[ny][nx] == INF) {
          dist[ny][nx] = dist[y][x] + 1;
          q.emplace(ny, nx);
        }
      }
  }
  int ans = INF;
  for (int i = (0); i < (h); ++i)
    ans = min(ans, min(dist[i][0], dist[i][w - 1]));
  for (int i = (0); i < (w); ++i)
    ans = min(ans, min(dist[0][i], dist[h - 1][i]));
  cout << ans + 1;
  return 0;
}
