#include <iostream>
#include <queue>
using namespace std;

int dx[] = { 1, -1, 0,  0 };
int dy[] = { 0,  0, 1, -1 };

int main() {
  int height, width, k;

  cin >> height
      >> width
      >> k;

  int startHeight;
  int startWidth;
  char grid[height][width];

  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == 'S') {
        startHeight = i;
        startWidth = j;
      }
    }
  }

  int min = 100000;
  int board[height][width];
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      board[i][j] = -1;
    }
  }

  board[statHeight][startWidth] = 0;
  queue<int> queueX;
  queue<int> queueY;
  queueX.push(startHeight);
  queueY.push(startWidth);
 while (queueX.size() > 0) {
    int x = queueX.front();
    int y = queueY.front();
    queueX.pop();
    queueY.pop();

    for (int i = 0; i < dx.size(); i++) {
      int nextX = x;
      int nextY = y;
      int majic = k;
      for (int j = 0; j < k; j++) {
        if (   grid[nextY + dy[i]][nextX + dx[i]] == '.'
            || grid[nextY + dy[i]][nextX + dx[i]] == 'S') {
          nextX += dx[i];
          nextY += dy[i];
        } else if (grid[nextY + dy[i]][nextX + dx[i]]] == "#" && k > 0) {
          nextX += dx[i];
          nextY += dy[i];
          k--;
        }
      }
      if (   0 <= nextX && nextX < width
          && 0 <= nextY && nextY < height
          && board[nextY][nextX] == -1) {
        board[nextY][nextX] = board[y][x] + 1;
        queueX.push(nextX);
        queueY.push(nextY);
      }
    }
  }
 for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      if (grid[i][j] == "." && board[i][j] == -1)
        return -1;
      min = std::min(min, board[i][j]);
    }
  }

  return 0;
}