#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);
  queue<pair<int, int> > que;
  for (int i = 0; i < h; i++) {
    dp[i][0] = dp[i][w - 1] = 0;
    que.push(make_pair(i, 0));
    que.push(make_pair(i, w - 1));
  }
  for (int i = 0; i < w; i++) {
    dp[0][i] = dp[h - 1][i] = 0;
    que.push(make_pair(0, i));
    que.push(make_pair(h - 1, i));
  }
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  while (que.size()) {
    auto f = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = f.second + dx[i], ny = f.first + dy[i];
      if (nx >= 0 && nx < w && ny >= 0 && ny < h) {
        int nv = dp[f.first][f.second] + 1 + (r[f.first][f.second] == '#');
        if (nv < dp[ny][nx]) {
          dp[ny][nx] = nv;
          que.push(make_pair(ny, nx));
        }
      }
    }
  }
  cout << min(k, dp[sa][sb]) << endl;
}
