#include <bits/stdc++.h>
using namespace std;
struct state {
  int y, x, d;
};
int H, W, K;
char A[800][800];
int sy, sx;
bool visited[800][800];
int dd[5] = {0, 1, 0, -1, 0};
bool isin(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }
bool isgoal(int y, int x) {
  return y == 0 || y == H - 1 || x == 0 || x == W - 1;
}
int main(void) {
  cin >> H >> W >> K;
  for (int i = (0); i < H; i++) {
    cin >> A[i];
    for (int j = (0); j < W; j++) {
      if (A[i][j] == 'S') {
        A[i][j] = '.';
        sy = i;
        sx = j;
      }
    }
  }
  for (int i = (0); i < H; i++)
    for (int j = (0); j < W; j++) visited[i][j] = false;
  queue<struct state> q1, q2;
  struct state first = {sy, sx, 0};
  q1.push(first);
  int cnt = 1;
  while (1) {
    while (!q1.empty()) {
      struct state c = q1.front();
      q1.pop();
      visited[c.y][c.x] = true;
      if (isgoal(c.y, c.x)) goto FOUND;
      for (int i = (0); i < 4; i++) {
        struct state n = {c.y + dd[i], c.x + dd[i + 1], c.d + 1};
        if (n.d <= K && isin(n.y, n.x) && !visited[n.y][n.x]) {
          if (A[n.y][n.x] == '.')
            q1.push(n);
          else {
            struct state m = {n.y, n.x, 0};
            q2.push(m);
          }
        }
      }
    }
    cnt++;
    while (!q2.empty()) {
      struct state c = q2.front();
      q2.pop();
      visited[c.y][c.x] = true;
      if (isgoal(c.y, c.x)) goto FOUND;
      for (int i = (0); i < 4; i++) {
        struct state n = {c.y + dd[i], c.x + dd[i + 1], c.d + 1};
        if (n.d <= K && isin(n.y, n.x) && !visited[n.y][n.x]) {
          if (A[n.y][n.x] == '#')
            q2.push(n);
          else {
            struct state m = {n.y, n.x, 0};
            q1.push(m);
          }
        }
      }
    }
  }
FOUND:
  cout << cnt << endl;
  return 0;
}
