#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char grid[1000][1000];
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
long long int cost[1000][1000];
long long int calc(int x, int y) {
  int mn;
  mn = min(x, y);
  mn = min(mn, h - 1 - x);
  mn = min(mn, w - 1 - y);
  return mn;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  for (int i = 0; i < 1000; i++) {
    for (int j = 0; j < 1000; j++) cost[i][j] = 1e18;
  }
  long long int ans = 1e18;
  queue<int> q;
  cin >> h >> w >> k;
  int startx = -1, starty = -1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == 'S') {
        grid[i][j] = '.';
        startx = i;
        starty = j;
      }
    }
  }
  cost[startx][starty] = 0;
  q.push(startx);
  q.push(starty);
  ans = min(ans, calc(startx, starty));
  int tx, ty;
  while (!q.empty()) {
    tx = q.front();
    q.pop();
    ty = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tdx, tdy;
      tdx = tx + dx[i];
      tdy = ty + dy[i];
      if (tdx < 0 || tdy < 0 || tdx >= h || tdy >= w) {
        continue;
      }
      if (grid[tdx][tdy] == '#') {
        continue;
      }
      if (cost[tdx][tdy] != 1e19) {
        continue;
      }
      cost[tdx][tdy] = cost[tx][ty] + 1;
      if (cost[tdx][tdy] == k) continue;
      q.push(tdx);
      q.push(tdy);
    }
  }
  cout << ans + 1 << '\n';
  return 0;
}
