#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const double PI = acos(-1.0);
int w, h, k;
char a[801][801];
int sy, sx;
struct Data {
  int y, x, idou, kaijo, mahou;
};
int bfs() {
  queue<Data> q;
  int ans;
  q.push({sy, sx, 0, 0, 0});
  while (!q.empty()) {
    int nowy = q.front().y;
    int nowx = q.front().x;
    int nowidou = q.front().idou;
    int nowkaijo = q.front().kaijo;
    int nowmahou = q.front().mahou;
    a[nowy][nowx] = '?';
    if (nowy == 0 || nowx == 0 || nowy == h - 1 || nowx == w - 1) {
      ans = nowmahou;
      break;
    }
    q.pop();
    for (int i = -1; i < 2; i++) {
      for (int j = -1; j < 2; j++) {
        if (i ^ j == 0 || i ^ j == 2) continue;
        if (nowidou < k && a[nowy + i][nowx + j] == '.') {
          if (nowidou == 0)
            q.push({nowy + i, nowx + j, nowidou + 1, 0, nowmahou + 1});
          else
            q.push({nowy + i, nowx + j, nowidou + 1, 0, nowmahou});
        } else if (nowkaijo < k && a[nowy + i][nowx + j] == '#') {
          a[nowy + i][nowx + j] = '.';
          if (nowkaijo == 0 && nowidou == 0)
            q.push({nowy, nowx, nowidou, nowkaijo + 1, nowmahou + 1});
          else
            q.push({nowy, nowx, nowidou, nowkaijo + 1, nowmahou});
        } else if (a[nowy + i][nowx + j] != '#') {
          q.push({nowy, nowx, 0, 0, nowmahou + 1});
        } else if (nowkaijo >= k && nowmahou >= k) {
          q.push({nowy, nowx, 0, 0, nowmahou + 1});
        }
      }
    }
  }
  return ans;
}
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') sy = i, sx = j;
    }
  }
  cout << bfs() << endl;
  return 0;
}
