#include <bits/stdc++.h>
using namespace std;
int H, W, K;
string A[801];
int ans;
int calc(vector<char> vs) {
  int pos = 0;
  int cnt = 0;
  int spos = 0;
  int scnt = 0;
  int a = 0;
  while (true) {
    while (pos < vs.size()) {
      if (vs[pos] == '.') {
        pos++;
        cnt++;
        if (cnt == K) break;
      } else
        break;
    }
    while (spos < vs.size()) {
      if (vs[spos] == '#') {
        vs[spos] = '.';
        scnt++;
        if (scnt == K) break;
      }
      spos++;
    }
    a++;
    if (pos >= vs.size()) return a;
  }
}
int vh[] = {1, 0, -1, 0};
int vw[] = {0, 1, 0, -1};
void dfs(int h, int w, bool used[801][801], vector<char> vs) {
  if (h == 0 || h == H - 1 || w == 0 || w == W - 1) {
    ans = min(ans, calc(vs));
    return;
  }
  for (int i = (0); i < (4); i++) {
    int nh = h + vh[i];
    int nw = w + vw[i];
    if (0 <= nh && nh < H && 0 <= nw && nw < W && !used[nh][nw]) {
      used[nh][nw] = true;
      vs.push_back(A[nh][nw]);
      dfs(nh, nw, used, vs);
      used[nh][nw] = false;
      vs.pop_back();
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> H >> W >> K;
  for (int i = (0); i < (H); i++) {
    cin >> A[i];
  }
  int sh, sw;
  for (int i = (0); i < (H); i++) {
    for (int j = (0); j < (W); j++) {
      if (A[i][j] == 'S') {
        sh = i;
        sw = j;
      }
    }
  }
  ans = 1e9;
  bool used[801][801] = {};
  used[sh][sw] = true;
  vector<char> vs;
  dfs(sh, sw, used, vs);
  cout << ans << endl;
}
