#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int H, W, K;
string S[808];
int A[808][808];
const int INF = 1e9;
priority_queue<pair<int, pair<int, int> > > pq;
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
void solve(int k, int r, int c, int d) {
  if (k == 0) return;
  for (int i = 0; i < (4); i++) {
    int rr = r + dr[i], cc = c + dc[i];
    if (rr >= H || rr < 0 || cc >= W || cc < 0) continue;
    if ((S[rr][cc] == '.' && d % 2 == 0) || (S[rr][cc] == '#' && d % 2 == 1)) {
      if (A[rr][cc] < d + 1) continue;
      A[rr][cc] = d + 1;
      pq.push({d + 1, {rr, cc}});
      solve(k - 1, rr, cc, d);
    } else {
      if (A[rr][cc] < d + 2) continue;
      A[rr][cc] = d + 2;
      pq.push({d + 2, {rr, cc}});
    }
  }
}
int main() {
  for (int i = 0; i < (808); i++)
    for (int j = 0; j < (808); j++) A[i][j] = INF;
  cin >> H >> W >> K;
  int sr, sc;
  for (int i = 0; i < (H); i++) {
    cin >> S[i];
    for (int j = 0; j < (W); j++)
      if (S[i][j] == 'S') {
        sr = i;
        sc = j;
        A[i][j] = 0;
      }
  }
  pq.push({0, {sr, sc}});
  while (pq.size()) {
    pair<int, pair<int, int> > P = pq.top();
    pq.pop();
    int d = P.first, r = P.second.first, c = P.second.second;
    solve(K, r, c, d);
  }
  int mi = INF;
  for (int i = 0; i < (W); i++) mi = min(mi, min(A[0][i], A[H - 1][i]));
  for (int i = 0; i < (H); i++) mi = min(mi, min(A[i][0], A[i][W - 1]));
  cout << mi / 2 + 1 << endl;
  return 0;
}
