#include <bits/stdc++.h>
using namespace std;
struct Node {
  int x, y, step;
  bool operator<(const Node& a) const { return step > a.step; }
};
const int INF = 0x7fffffff;
const int maxn = 1e3;
char G[maxn][maxn];
int h, w, k, ans, s_x, s_y;
int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool vis[maxn][maxn];
void bfs() {
  priority_queue<Node> q;
  q.push(Node{s_x, s_y, 0});
  Node f;
  int x_t, y_t;
  while (!q.empty()) {
    f = q.top();
    q.pop();
    if (f.x == 0 || f.x == h - 1 || f.y == 0 || f.y == w - 1) {
      ans = min(ans, f.step);
      continue;
    }
    for (int i = 0; i < (4); ++i) {
      x_t = f.x + dir[i][0];
      y_t = f.y + dir[i][1];
      if (vis[x_t][y_t]) {
        continue;
      }
      if (G[x_t][y_t] == '.' && f.step + 1 <= k) {
        vis[x_t][y_t] = true;
        q.push(Node{x_t, y_t, f.step + 1});
      } else if (G[x_t][y_t] == '#' && f.step + 2 <= k) {
        q.push(Node{x_t, y_t, f.step + 2});
      }
    }
  }
}
int main() {
  scanf("%d %d %d", &h, &w, &k);
  ans = k;
  memset(vis, false, sizeof(vis));
  for (int i = 0; i < (h); ++i) {
    scanf("%s", G[i]);
    for (int j = 0; j < w; ++j) {
      if (G[i][j] == 'S') {
        s_x = i;
        s_y = j;
      }
    }
  }
  bfs();
  printf("%d\n", ans);
  return 0;
}
