#include <bits/stdc++.h>
using namespace std;
struct data {
  int x, y, tag, bu, dist;
  data() {}
  data(int x, int y, int tag, int bu, int dist)
      : x(x), y(y), tag(tag), bu(bu), dist(dist) {}
} q[1000100];
int dist[810][810], k, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, w, h, l, r;
char s[810][810];
int main() {
  scanf("%d%d%d", &w, &h, &k);
  for (int i = 1; i <= w; ++i) scanf("%s", s[i] + 1);
  for (int i = 1; i <= w; ++i)
    for (int j = 1; j <= h; ++j)
      if (s[i][j] == 'S') q[r++] = data(i, j, 0, 0, 2), dist[i][j] = 1;
  while (l < r) {
    data d = q[l++];
    for (int i = 0; i < 4; ++i) {
      int nx = d.x + dx[i], ny = d.y + dy[i];
      if (nx < 1 || ny < 1 || nx > w || ny > h) continue;
      if (dist[nx][ny] <= d.dist && dist[nx][ny]) continue;
      if (d.tag == 0) {
        if (s[nx][ny] == '#')
          q[r++] = data(nx, ny, 1, 1, d.dist), dist[nx][ny] = d.dist;
        else if (d.bu < k)
          q[r++] = data(nx, ny, 0, d.bu + 1, d.dist), dist[nx][ny] = d.dist;
        else if (dist[nx][ny] > d.dist + 1 || !dist[nx][ny])
          q[r++] = data(nx, ny, 0, 1, d.dist + 1), dist[nx][ny] = d.dist + 1;
      } else {
        if (s[nx][ny] == '.') {
          if (dist[nx][ny] > d.dist + 1 || !dist[nx][ny])
            q[r++] = data(nx, ny, 1, 1, d.dist + 1), dist[nx][ny] = d.dist + 1;
        } else if (d.bu < k)
          q[r++] = data(nx, ny, 1, d.bu + 1, d.dist), dist[nx][ny] = d.dist;
        else if (dist[nx][ny] > d.dist + 1 || !dist[nx][ny])
          q[r++] = data(nx, ny, 1, 1, d.dist + 1), dist[nx][ny] = d.dist + 1;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= w; ++i) {
    if (dist[i][1]) {
      if (!ans || ans > dist[i][1]) ans = dist[i][1];
    }
    if (dist[i][h]) {
      if (!ans || ans > dist[i][h]) ans = dist[i][h];
    }
  }
  for (int i = 1; i <= h; ++i) {
    if (dist[1][i]) {
      if (!ans || ans > dist[1][i]) ans = dist[w][i];
    }
    if (dist[w][i]) {
      if (!ans || ans > dist[w][i]) ans = dist[w][i];
    }
  }
  printf("%d", ans);
}
