#include <bits/stdc++.h>
using namespace std;
struct ROOM {
  long long x, y;
};
queue<ROOM> q, q2, q3;
long long chk[880][880], chk2[880][880];
string a[880];
long long h, w, k, sx, sy, v, vv, ans;
long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
void prt1() {
  for (int i = 0; i < h; i++, cout << endl)
    for (int j = 0; j < w; j++) cout << chk[i][j] << " ";
  cout << "----------------------------" << endl;
  int k;
  cin >> k;
}
void prt2() {
  for (int i = 0; i < h; i++, cout << endl)
    for (int j = 0; j < w; j++) cout << chk2[i][j] << " ";
  cout << "----------------------------" << endl;
  for (int i = 0; i < h; i++) cout << a[i] << endl;
  cout << "----------------------------" << endl;
  int k;
  cin >> k;
}
inline bool valid(long long x, long long y) {
  return (0 <= x && x < h) and (0 <= y && y < w);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  for (long long i = 0; i < h; i++) {
    cin >> a[i];
    for (long long j = 0; j < w; j++)
      if (a[i][j] == 'S') sx = i, sy = j;
  }
  q.push({sx, sy}), chk[sx][sy] = 1, v = chk[sx][sy];
  while (true) {
    ans++;
    while (!q.empty()) {
      ROOM now = q.front();
      q.pop();
      if (chk[now.x][now.y] > k + 1) {
        q3.push(now);
        chk[now.x][now.y] = 1;
        continue;
      }
      if (now.x == 0 or now.x == h - 1 or now.y == 0 or now.y == w - 1)
        return cout << ans << endl, 0;
      for (long long i = 0; i < 4; i++) {
        long long nx = now.x + dx[i], ny = now.y + dy[i];
        if (!valid(nx, ny)) continue;
        if (!chk[nx][ny]) {
          if (a[nx][ny] == '.')
            q.push({nx, ny}), chk[nx][ny] = chk[now.x][now.y] + 1;
          else
            q2.push({nx, ny}), chk2[nx][ny] = chk[nx][ny] = 1;
        }
      }
    }
    while (!q3.empty()) q.push(q3.front()), q3.pop();
    while (!q2.empty()) {
      ROOM now = q2.front();
      q2.pop();
      if (chk2[now.x][now.y] > k + 1) {
        chk2[now.x][now.y] = 0;
        continue;
      }
      a[now.x][now.y] = '.';
      if (chk2[now.x][now.y] == 1) q.push(now);
      for (long long i = 0; i < 4; i++) {
        long long nx = now.x + dx[i], ny = now.y + dy[i];
        if (!valid(nx, ny)) continue;
        if (!chk2[nx][ny]) {
          if (a[nx][ny] == '#')
            q2.push({nx, ny}), chk2[nx][ny] = chk2[now.x][now.y] + 1;
        }
      }
    }
  }
  return 0;
}
