#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int h, w, k;
char r[900][900];
int dp[900][900];
int main() {
  cin >> h >> w >> k;
  int sa, sb;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> r[i][j];
      if (r[i][j] == 'S') {
        sa = i, sb = j;
      }
    }
  }
  memset(dp, sizeof(dp), 1000000);
  for (int i = 0; i < h; i++) {
    dp[i][0] = dp[i][w - 1] = 0;
  }
  for (int i = 0; i < w; i++) {
    dp[0][i] = dp[h - 1][i] = 0;
  }
  for (int i = 1; i < h; i++) {
    for (int j = 1; j < w; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1 + (r[i - 1][j] == '#'));
      dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1 + (r[i][j - 1] == '#'));
    }
  }
  for (int i = h - 2; i >= 0; i--) {
    for (int j = w - 2; j >= 0; j--) {
      dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1 + (r[i + 1][j] == '#'));
      dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1 + (r[i][j + 1] == '#'));
    }
  }
  cout << min(k, dp[sa][sb]) << endl;
}
