#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char maze[805][805];
int vis[805][805][30];
int d[805][805];
int n, m, k, t;
int sx, sy;
struct Point {
  int x, y;
  int step;
  int layer;
  Point(int x = 1, int y = 1, int step = 0, int layer = 0)
      : x(x), y(y), step(step), layer(layer) {}
};
int bfs() {
  queue<Point> Q;
  memset(vis, 0, sizeof(vis));
  Point a(sx, sy, 0, 0);
  Point end_point(n, m);
  Q.push(a);
  vis[1][1][0] = 1;
  while (!Q.empty()) {
    Point now = Q.front();
    Q.pop();
    if (now.x == 0 || now.y == 0 || now.x == n || now.y == m)
      return now.step + 1;
    for (int i = 0; i < 4; i++) {
      int x = dx[i] + now.x;
      int y = dy[i] + now.y;
      int layer = now.layer;
      if (maze[x][y] == '#') {
        layer++;
      } else
        layer = 0;
      if (layer <= k && !vis[x][y][layer] && x >= 1 && x <= n && y <= m &&
          y >= 1) {
        vis[x][y][layer] = 1;
        Q.push(Point(x, y, now.step + 1, layer));
      }
    }
  }
  return -1;
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> maze[i][j];
      if (maze[i][j] == 'S') {
        sx = i, sy = j;
        maze[i][j] = '.';
      }
    }
  cout << bfs() << endl;
}
