#include <bits/stdc++.h>
using namespace std;
int DX[4] = {-1, 0, 1, 0};
int DY[4] = {0, -1, 0, 1};
int H, W, K;
int SX, SY;
char A[800][800];
bool D[800][800];
set<pair<int, int> > used[800][800];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W >> K;
  for (int i = 0; i < (H); i++) {
    for (int j = 0; j < (W); j++) {
      char c;
      cin >> c;
      A[j][i] = c == '#';
      if (c == 'S') SX = j, SY = i;
    }
  }
  queue<pair<pair<int, int>, pair<int, int> > > q;
  q.push(pair<pair<int, int>, pair<int, int> >(pair<int, int>(SX, SY),
                                               pair<int, int>(K, 0)));
  for (int steps = 1; steps < 500; steps++) {
    queue<pair<pair<int, int>, pair<int, int> > > nq;
    while (!q.empty()) {
      int x = q.front().first.first, y = q.front().first.second,
          moves = q.front().second.first, brks = q.front().second.second;
      q.pop();
      nq.push(pair<pair<int, int>, pair<int, int> >(
          pair<int, int>(x, y),
          pair<int, int>(min(1000, moves + K), min(450, brks + K))));
      if (x == 0 || x == W - 1 || y == 0 || y == H - 1) {
        cout << steps << "\n";
        return 0;
      }
      if (moves > 0) {
        for (int k = 0; k < (4); k++) {
          int nx = x + DX[k], ny = y + DY[k];
          if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
          int nb = brks;
          if (A[nx][ny]) {
            if (nb == 0) continue;
            nb--;
          }
          if (D[nx][ny]) continue;
          D[nx][ny] = true;
          auto p = pair<pair<int, int>, pair<int, int> >(
              pair<int, int>(nx, ny), pair<int, int>(moves - 1, nb));
          if (used[nx][ny].find(pair<int, int>(moves - 1, nb)) !=
              used[nx][ny].end())
            continue;
          used[nx][ny].insert(pair<int, int>(moves - 1, nb));
          q.push(pair<pair<int, int>, pair<int, int> >(
              pair<int, int>(nx, ny), pair<int, int>(moves - 1, nb)));
        }
      }
    }
    swap(q, nq);
  }
  return 0;
}
