#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000009;
const int MOD = 1000000007;
const double EPS = 1e-14;
const double PI = acos(-1);
const int MAX = 800;
int H, W, K;
bool A[MAX][MAX];
bool V[MAX][MAX];
bool B[MAX][MAX];
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {-1, 0, 1, 0};
int main() {
  cin >> H >> W >> K;
  queue<pair<int, int> > q;
  for (long long h = 0; h < (H); ++h)
    for (long long w = 0; w < (W); ++w) B[h][w] = V[h][w] = false;
  for (long long h = 0; h < (H); ++h) {
    string s;
    cin >> s;
    for (long long w = 0; w < (W); ++w) {
      A[h][w] = (s[w] != '#');
      if (s[w] == 'S') q.push(make_pair(h, w));
    }
  }
  int ans = 0;
  while (1) {
    ans++;
    int d = 0;
    pair<int, int> l = q.back();
    queue<pair<int, int> > closed;
    while (!q.empty()) {
      pair<int, int> p = q.front();
      q.pop();
      int h = p.first, w = p.second;
      if (A[h][w]) {
        V[h][w] = true;
        if (h == 0 || h == H || w == 0 || w == W) {
          cout << ans << endl;
          return 0;
        }
        for (long long i = 0; i < (4); ++i) {
          int x = dx[i], y = dy[i];
          if (!V[h + y][w + x]) q.push(make_pair(h + y, w + x));
          if (!A[h + y][w + x]) closed.push(make_pair(h + y, w + x));
        }
      } else {
        q.push(p);
      }
      if (l.first == h && l.second == w) {
        d++;
        l = q.back();
        if (d > K) break;
      }
    }
    d = 0;
    l = closed.back();
    while (!closed.empty()) {
      pair<int, int> p = closed.front();
      closed.pop();
      int h = p.first, w = p.second;
      A[h][w] = true;
      for (long long i = 0; i < (4); ++i) {
        int x = dx[i], y = dy[i];
        if (h + y >= H || h + y < 0 || w + x >= W || w + x < 0) continue;
        if (!A[h + y][w + x] && !B[h + y][w + x]) {
          closed.push(make_pair(h + y, w + x));
          B[h + y][w + x] = true;
        }
      }
      if (l.first == h && l.second == w) {
        d++;
        l = closed.back();
        if (d == K) break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
