#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, m, k, nowa, nowb;
char ch[807][807];
int f[807][807][2], a[807][807], dis[807][807];
bool flag[807][807], flag1[807][807][2];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int dfs1(int x, int y) {
  if (flag[x][y]) return dis[x][y];
  flag[x][y] = true;
  if (a[x][y] == 1) return 20000007;
  for (int i = 0; i < 4; i++) {
    int rx = x + dx[i], ry = y + dy[i];
    if (rx >= 1 && rx <= n && ry >= 1 && ry <= m && a[rx][ry] == 0)
      dis[x][y] = min(dis[x][y], dfs1(rx, ry) + 1);
  }
  return dis[x][y];
}
int dfs(int x, int y, int flag) {
  if (x == 1 || x == n || y == 1 || y == m) return 0;
  if (flag1[x][y][flag]) return f[x][y][flag];
  flag1[x][y][flag] = true;
  if (flag) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (dis[i][j] <= k)
          f[x][y][flag] = min(f[x][y][flag], dfs(i, j, 0) + 1);
  } else {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (abs(i - x) + abs(j - y) <= k)
          f[x][y][flag] = min(f[x][y][flag], dfs(i, j, 0) + 1);
  }
  return f[x][y][flag];
}
int main() {
  n = read(), m = read(), k = read();
  memset(f, 0, sizeof(f));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ch[i][j] = getchar();
      if (ch[i][j] == '#')
        a[i][j] = 1;
      else
        a[i][j] = 0;
      if (ch[i][j] == 'S') nowa = i, nowb = j;
    }
    getchar();
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      f[i][j][0] = f[i][j][1] = 20000007, dis[i][j] = 20000007;
  dis[nowa][nowb] = 0;
  memset(flag, false, sizeof(flag));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 0) dis[i][j] = dfs1(i, j);
  memset(flag1, false, sizeof(flag1));
  printf("%d\n", dfs(nowa, nowb, 1));
  return 0;
}
