#include <bits/stdc++.h>
using namespace std;
void init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int n, m, k;
char arr[805][805];
int fille[805][805];
int dr[] = {0, 1, 0, -1};
int dc[] = {1, 0, -1, 0};
void traverse(int x, int y, int rem, int now) {
  fille[x][y] = now;
  for (long long int(i) = (0); (i) < (4); ++(i)) {
    if (x + dr[i] < 0 || x + dr[i] == n || y + dc[i] < 0 || y + dc[i] > m)
      continue;
    if (arr[x + dr[i]][y + dc[i]] == '.' && fille[x + dr[i]][y + dc[i]] == -1) {
      if (arr[x][y] == '#')
        traverse(x + dr[i], y + dc[i], k, now + 1);
      else if (rem > 0)
        traverse(x + dr[i], y + dc[i], rem - 1, now);
      else
        traverse(x + dr[i], y + dc[i], k, now + 1);
    } else if (arr[x + dr[i]][y + dc[i]] == '#' &&
               fille[x + dr[i]][y + dc[i]] == -1) {
      if (arr[x][y] == '.' || arr[x][y] == 'S')
        traverse(x + dr[i], y + dc[i], k, now + 2);
      else if (rem > 0)
        traverse(x + dr[i], y + dc[i], rem - 1, now);
      else
        traverse(x + dr[i], y + dc[i], k, now + 1);
    }
  }
}
int main() {
  init();
  cin >> n >> m >> k;
  int x, y;
  for (long long int(i) = (0); (i) < (n); ++(i)) {
    for (long long int(j) = (0); (j) < (m); ++(j)) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  for (long long int(i) = (0); (i) < (805); ++(i)) {
    for (long long int(j) = (0); (j) < (805); ++(j)) fille[i][j] = -1;
  }
  traverse(x, y, k, 0);
  int mins = INT_MAX;
  for (long long int(i) = (0); (i) < (n); ++(i)) {
    mins = min(mins, fille[i][0]);
  }
  for (long long int(i) = (0); (i) < (n); ++(i)) {
    mins = min(mins, fille[i][m - 1]);
  }
  for (long long int(i) = (0); (i) < (m); ++(i)) {
    mins = min(mins, fille[0][i]);
  }
  for (long long int(i) = (0); (i) < (m); ++(i)) {
    mins = min(mins, fille[n - 1][i]);
  }
  cout << mins << '\n';
  return 0;
}
