#include <bits/stdc++.h>
using namespace std;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x7f7f7f7f;
int n, m, k;
char mapp[805][805];
int vis[805][805];
int dic[][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
struct node {
  int x, y, step;
};
vector<int> vec;
void bfs(int x, int y) {
  vis[x][y] = INF;
  queue<node> q;
  node p1;
  p1.x = x, p1.y = y, p1.step = 0;
  q.push(p1);
  while (!q.empty()) {
    node p = q.front();
    q.pop();
    if (p.step > k) continue;
    for (int i = 0; i < 4; i++) {
      int xx = p.x + dic[i][0];
      int yy = p.y + dic[i][1];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m) {
        if (xx == 0 || xx == n - 1 || yy == 1 || yy == m - 1) {
          if (mapp[xx][yy] == '#') {
            vis[xx][yy] = min(p.step + 2, vis[xx][yy]);
            vec.push_back(vis[xx][yy]);
          } else {
            vis[xx][yy] = min(p.step + 1, vis[xx][yy]);
            vec.push_back(vis[xx][yy]);
          }
        } else if (mapp[xx][yy] == '#' && p.step + 2 < vis[xx][yy]) {
          vis[xx][yy] = p.step + 2;
          node p2;
          p2.x = xx;
          p2.y = yy;
          p2.step = p.step + 2;
          q.push(p2);
        } else if (mapp[xx][yy] == '.' && p.step + 1 < vis[xx][yy]) {
          vis[xx][yy] = p.step + 1;
          node p2;
          p2.x = xx;
          p2.y = yy;
          p2.step = p.step + 1;
          q.push(p2);
        }
      }
    }
  }
  sort(vec.begin(), vec.end());
  printf("%d\n", min(*vec.begin(), k));
}
int main() {
  memset(vis, INF, sizeof(vis));
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%s", mapp[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (mapp[i][j] == 'S') {
        bfs(i, j);
        return 0;
      }
    }
  return 0;
}
