#include <bits/stdc++.h>
using namespace std;
int mat[1000][1000];
int memo[1000][1000];
int x, y, n, m, k;
void BFS() {
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  memo[x][y] = 1;
  while (!q.empty()) {
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (memo[x][y] == 0) memo[x][y] = 3;
    for (int i = 1; i <= k; i++) {
      if (x + i < n) {
        if (memo[x + i][y] == 0) {
          if (mat[x + i][y] == 1)
            memo[x + i][y] = memo[x][y] + 1;
          else
            mat[x + i][y] = 1;
          q.push(make_pair(x + i, y));
        }
      }
      if (x - i >= 0) {
        if (memo[x - i][y] == 0) {
          if (mat[x - i][y] == 1)
            memo[x - i][y] = memo[x][y] + 1;
          else
            mat[x - i][y] = 1;
          q.push(make_pair(x - i, y));
        }
      }
      if (y + i < m) {
        if (memo[x][y + i] == 0) {
          if (mat[x][y + i] == 1)
            memo[x][y + i] = memo[x][y] + 1;
          else
            mat[x][y + i] = 1;
          q.push(make_pair(x, y + i));
        }
      }
      if (y - i >= 0) {
        if (memo[x][y - i] == 0) {
          if (mat[x][y - i] == 1)
            memo[x][y - i] = memo[x][y] + 1;
          else
            mat[x][y - i] = 1;
          q.push(make_pair(x, y - i));
        }
      }
    }
  }
}
int main() {
  int step = 0, i = 0, j = 0;
  string s = "";
  cin >> n >> m >> k;
  for (i = 0; i < n; i++) {
    cin >> s;
    for (j = 0; j < m; j++) {
      if (s[j] == '#')
        mat[i][j] = 0;
      else
        mat[i][j] = 1;
      if (s[j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  BFS();
  step = INT_MAX;
  for (i = 0; i < m; i++) {
    step = min(step, memo[0][i]);
    step = min(step, memo[n - 1][i]);
  }
  step--;
  cout << step << endl;
  return 0;
}
