#include <bits/stdc++.h>
using namespace std;
const int MaxN = 800;
int n, m, k;
int cast[MaxN + 5][MaxN + 5], tim[MaxN + 5][MaxN + 5];
char a[MaxN + 5][MaxN + 5];
int dx[5] = {0, -1, 0, 1, 0}, dy[5] = {0, 0, 1, 0, -1};
bool unlock[MaxN + 5][MaxN + 5], vis[MaxN + 5][MaxN + 5];
struct Node {
  int x, y;
} s;
vector<Node> p, seq;
bool Check(int x, int y) {
  if (x < 1 || x > n) return 0;
  if (y < 1 || y > m) return 0;
  return 1;
}
void Dfs(int x, int y, int dep, int temp) {
  if (dep > k || unlock[x][y]) return;
  unlock[x][y] = 1;
  cast[x][y] = temp + 1;
  Node now;
  now.x = x;
  now.y = y;
  p.push_back(now);
  for (int i = 1; i <= 4; i++)
    if (Check(x + dx[i], y + dy[i])) Dfs(x + dx[i], y + dy[i], dep + 1, temp);
}
void Dfs1(int x, int y, int dep, int temp) {
  if (dep > k || vis[x][y]) return;
  vis[x][y] = 1;
  tim[x][y] = max(cast[x][y] + 1, temp + 1);
  Node now;
  now.x = x;
  now.y = y;
  seq.push_back(now);
  if (cast[x][y] > temp) return;
  for (int i = 1; i <= 4; i++)
    if (Check(x + dx[i], y + dy[i])) Dfs1(x + dx[i], y + dy[i], dep + 1, temp);
}
void Bfs(Node s) {
  memset(unlock, 0, sizeof(unlock));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] != '#') {
        unlock[i][j] = 1;
        Node now;
        now.x = i;
        now.y = j;
        p.push_back(now);
        cast[i][j] = 0;
      } else
        cast[i][j] = 1 << 30;
    }
  }
  int pos = 0;
  while (pos < p.size()) {
    Node now = p[pos];
    for (int i = 1; i <= 4; i++)
      if (Check(now.x + dx[i], now.y + dy[i]))
        Dfs(now.x + dx[i], now.y + dy[i], 1, cast[now.x][now.y]);
    pos++;
  }
  memset(vis, 0, sizeof(vis));
  memset(tim, 60, sizeof(tim));
  int head = 0;
  seq.push_back(s), tim[s.x][s.y] = 0;
  vis[s.x][s.y] = 1;
  while (head < seq.size()) {
    Node now = seq[head];
    for (int i = 1; i <= 4; i++)
      if (Check(now.x + dx[i], now.y + dy[i]))
        Dfs1(now.x + dx[i], now.y + dy[i], 1, tim[now.x][now.y]);
    head++;
  }
  int ans = 1 << 30;
  for (int i = 1; i <= m; i++) ans = min(ans, tim[n][i]);
  for (int i = 1; i <= n; i++) ans = min(ans, tim[i][m]);
  printf("%d\n", ans);
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      scanf("%c", &a[i][j]);
      if (a[i][j] == 'S') s.x = i, s.y = j;
    }
  }
  Bfs(s);
}
