#include <bits/stdc++.h>
using namespace std;
int n, m, p, ans = 1e9;
char arr[805][805];
int x[4] = {0, 1, 0, -1};
int y[4] = {-1, 0, 1, 0};
int vis[805][805];
int dfs(int yo, int xo, int type, int move, int cnt) {
  if (yo == 0 || yo == n - 1 || xo == 0 || xo == m - 1) {
    return cnt;
  }
  for (int k = 0; k < 4; k++) {
    int xi = xo + x[k];
    int yi = yo + y[k];
    if (yo >= 0 && yo < n && xo >= 0 && xo < m) {
      if (vis[yi][xi] > cnt) {
        vis[yi][xi] = cnt;
        if (arr[yi][xi] == arr[yo][xo]) {
          if (move == 0) {
            ans = min(ans, dfs(yi, xo, type, p, cnt + 1));
          } else {
            ans = min(ans, dfs(yi, xo, type, move - 1, cnt));
          }
        } else {
          ans = min(ans, dfs(yi, xo, 1 - type, p, cnt + 1));
        }
      }
    }
  }
  return ans;
}
int main() {
  int xo, yo;
  cin >> n >> m >> p;
  memset(vis, 0x3f, sizeof(vis));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S') {
        yo = i;
        xo = j;
        arr[i][j] = '.';
      }
    }
  }
  cout << dfs(yo, xo, 0, p, 1) << endl;
  return 0;
}
