#include <bits/stdc++.h>
using namespace std;
bool debug = false;
char c[808][808];
int ans = 1e9;
int n, m, k;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
void dfs(int x, int y, int w, int b, int cast) {
  if (cast > ans) return;
  if (x == 0 || x == n - 1 || y == 0 || y == m - 1) {
    ans = min(ans, cast);
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (c[nx][ny] == '#' && b == 0) {
      c[nx][ny] = '.';
      dfs(x, y, w, k, cast + 1);
      c[nx][ny] = '#';
    } else {
      if (c[nx][ny] == '#') {
        c[nx][ny] = '.';
        dfs(x, y, w, b - 1, cast);
        c[nx][ny] = '#';
      }
    }
    if (c[nx][ny] == '.' && w == 0) {
      dfs(nx, ny, k, b, cast + 1);
    } else {
      if (c[nx][ny] == '.') {
        dfs(nx, ny, w - 1, b, cast);
      }
    }
  }
}
int main(int argc, char* argv[]) {
  cin >> n >> m >> k;
  pair<int, int> st;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'S') {
        st = make_pair(i, j);
      }
    }
  }
  dfs(st.first, st.second, 0, 0, 0);
  cout << ans;
  return 0;
}
