#include <bits/stdc++.h>
using namespace std;
void init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
struct pos {
  int x, y;
  int move, lock;
  int t;
};
int dr[] = {0, 1, 0, -1};
int dc[] = {1, 0, -1, 0};
char arr[805][805];
int visited[805][805];
int main() {
  init();
  int h, w, k;
  cin >> h >> w >> k;
  int x, y;
  for (long long int(i) = (0); (i) < (h); ++(i)) {
    for (long long int(j) = (0); (j) < (w); ++(j)) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  pos start;
  start.x = x;
  start.y = y;
  start.lock = k;
  start.move = k;
  start.t = 0;
  queue<pos> q;
  q.push(start);
  memset((visited), (0), sizeof((visited)));
  visited[x][y] = 1;
  pos another;
  int ans = k;
  while (q.empty() == false) {
    start = q.front();
    q.pop();
    if (start.x == 0 || start.x == h - 1 || start.y == w - 1 || start.y == 0) {
      ans = start.t;
      break;
    }
    for (long long int(i) = (0); (i) < (4); ++(i)) {
      another = start;
      another.x += dr[i];
      another.y += dc[i];
      if (arr[x + dr[i]][y + dc[i]] == '#') {
        if (visited[x + dr[i]][y + dc[i]] != 0 &&
            another.t >= visited[x + dr[i]][y + dc[i]] && another.move > 0) {
          another.move--;
          another.t++;
          q.push(another);
        } else if (visited[x + dr[i]][y + dc[i]] == 0 && another.lock > 0) {
          visited[x + dr[i]][y + dc[i]] = start.t + 1;
          another.lock--;
          another.x = start.x;
          another.y = start.y;
          another.t++;
          q.push(another);
        }
      } else if (arr[x + dr[i]][y + dc[i]] == '.' && another.move > 0) {
        if (visited[x + dr[i]][y + dc[i]] == 1) {
          visited[x + dr[i]][y + dc[i]] == 2;
          another.move--;
          another.t++;
          q.push(another);
        } else if (visited[x + dr[i]][y + dc[i]] == 0 && another.move > 0) {
          visited[x + dr[i]][y + dc[i]] = 1;
          another.move--;
          another.t++;
          q.push(another);
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
