#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  vector<string> b = vector<string>(h);
  for (int i = 0; i < h; ++i) cin >> b[i];
  vector<vector<int>> step(h, vector<int>(w, (1 << 30)));
  vector<vector<bool>> extended(h, vector<bool>(w, false));
  int sx, sy;
  bool find = false;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (b[i][j] == 'S') {
        find = true;
        sx = i;
        sy = j;
        break;
      }
    }
    if (find) break;
  }
  int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  step[sx][sy] = 0;
  queue<int> qx;
  queue<int> qy;
  qx.push(sx);
  qy.push(sy);
  while (!qx.empty()) {
    int x = qx.front();
    int y = qy.front();
    qx.pop();
    qy.pop();
    if (extended[x][y]) continue;
    for (int i = 0; i < 4; ++i) {
      int nx = x + dir[i][0];
      int ny = y + dir[i][1];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w) {
        int nstep = step[x][y] + (b[nx][ny] == '.' ? 1 : 2);
        if (nstep < step[nx][ny]) {
          step[nx][ny] = nstep;
          qx.push(nx);
          qy.push(ny);
        }
      }
    }
    extended[x][y] = true;
  }
  int ans = (1 << 30);
  int ex, ey;
  for (int i = 0; i < h; ++i) {
    if (i == 0 || i == h - 1) {
      for (int j = 0; j < w; ++j) {
        if (step[i][j] < ans) {
          ans = step[i][j];
          ex = i;
          ey = j;
        }
      }
    } else {
      if (step[i][0] < ans) {
        ans = step[i][0];
        ex = i;
        ey = 0;
      }
      if (step[i][w - 1] < ans) {
        ans = step[i][w - 1];
        ex = i;
        ey = w - 1;
      }
    }
  }
  int move = abs(sx - ex) + abs(sy - ey);
  if (move > k)
    cout << k << endl;
  else
    cout << ans << endl;
  return 0;
}
