#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const long long LINF = 1e18;
const double pi = 3.1415926535897932;
const string endstr = "\n";
template <typename T>
T gcd(T a, T b) {
  return (a == 0) ? b : gcd(b % a, a);
}
template <typename T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
bool p_comp_fs(const pair<long long, long long> p1,
               const pair<long long, long long> p2) {
  return p1.first < p2.first;
};
bool p_comp_fg(const pair<long long, long long> p1,
               const pair<long long, long long> p2) {
  return p1.first > p2.first;
};
bool p_comp_ss(const pair<long long, long long> p1,
               const pair<long long, long long> p2) {
  return p1.second < p2.second;
};
bool p_comp_sg(const pair<long long, long long> p1,
               const pair<long long, long long> p2) {
  return p1.second > p2.second;
};
template <typename T>
vector<T> uniquen(vector<T> vec) {
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  return vec;
}
template <class T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
long long H, W;
bool bok(long long h, long long w) {
  return 0 <= h && h < H && 0 <= w && w < W;
}
struct st {
  pair<long long, long long> pos;
  long long rem, cnt;
};
vector<long long> dh = {-1, 1, 0, 0};
vector<long long> dw = {0, 0, -1, 1};
int main() {
  long long K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (long long i = 0; i < H; i++) cin >> A[i];
  long long sh = -1, sw = -1;
  for (long long i = 0; i < H; i++)
    for (long long j = 0; j < W; j++)
      if (A[i][j] == 'S') {
        sh = i;
        sw = j;
        i = H;
        j = W;
      }
  queue<st> q;
  q.push({{sh, sw}, K, 1});
  vector<vector<long long> > cost(H, vector<long long>(W, LINF));
  cost[sh][sw] = 1;
  while (!q.empty()) {
    pair<long long, long long> now = q.front().pos;
    long long rem = q.front().rem, cnt = q.front().cnt;
    q.pop();
    if (rem == 0) {
      q.push({now, K, cnt + 1});
    } else if (cnt == 1) {
      bool updated = false;
      for (long long i = 0; i < 4; i++) {
        long long nh = now.first + dh[i], nw = now.second + dw[i];
        if (bok(nh, nw) && A[nh][nw] == '.' && cost[nh][nw] == LINF) {
          cost[nh][nw] = cnt;
          q.push({{nh, nw}, rem - 1, cnt});
          updated = true;
        }
      }
      if (!updated) q.push({now, rem - 1, cnt});
    } else {
      for (long long i = 0; i < 4; i++) {
        long long nh = now.first + dh[i], nw = now.second + dw[i];
        if (bok(nh, nw) && cost[nh][nw] == LINF) {
          cost[nh][nw] = cnt;
          q.push({{nh, nw}, rem - 1, cnt});
        }
      }
    }
  }
  long long ans = LINF;
  for (long long i = 0; i < W; i++) {
    chmin(ans, cost[0][i]);
    chmin(ans, cost[H - 1][i]);
  }
  for (long long i = 0; i < H; i++) {
    chmin(ans, cost[i][0]);
    chmin(ans, cost[i][W - 1]);
  }
  cout << ans << endstr;
  return 0;
}
