#include <bits/stdc++.h>
using namespace std;
struct cell {
  int x, y;
  cell(int x = 0, int y = 0) : x(x), y(y) {}
};
int H, W, K, d[810][810], sz, res;
char a[810][810];
cell q[2000000];
bool g = 0, add[810][810];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, -1, 1};
void dfs(int x, int y, int dist, int cnt, int mx, int val) {
  int k, nx, ny;
  if (x == 1 || x == H || y == 1 || y == W) {
    g = 1;
    res = val + 1;
  }
  if (dist == K || g) return;
  for (k = 0; k < 4; k++) {
    nx = x + dx[k], ny = y + dy[k];
    if (nx < 1 || nx > H || ny < 1 || ny > W) continue;
    if (!(a[nx][ny] == '#' && cnt == mx)) {
      if (val + 1 < d[nx][ny]) {
        d[nx][ny] = val + 1;
        q[++sz] = cell(nx, ny);
      }
      dfs(nx, ny, dist + 1, cnt + ((a[nx][ny] == '#') ? 1 : 0), mx, val);
    }
  }
}
int main() {
  cin >> H >> W >> K;
  int i, j, sx, sy;
  for (i = 1; i <= H; i++)
    for (j = 1; j <= W; j++) {
      cin >> a[i][j];
      d[i][j] = (1 << 30);
      if (a[i][j] == 'S') sx = i, sy = j, d[i][j] = 0;
    }
  dfs(sx, sy, 0, 0, 0, 0);
  dfs(sx, sy, 0, 0, K, 1);
  for (i = 1; i <= sz && !g; i++)
    dfs(q[i].x, q[i].y, 0, 0, K, d[q[i].x][q[i].y]);
  cout << res << "\n";
  return 0;
}
