#include <bits/stdc++.h>
using namespace std;
const int N = 805;
const int oo = 1e9;
int fx[] = {0, 0, -1, +1};
int fy[] = {-1, +1, 0, 0};
int n, m, k;
char g[N][N];
int dis[N][N];
int fix[N][N];
queue<pair<int, int> > nw;
bool valid(int x, int y) {
  if (x >= 0 && y >= 0 && x < n && y < m)
    return true;
  else
    return false;
}
bool process() {
  queue<pair<int, int> > q, Q;
  while (!nw.empty()) {
    pair<int, int> f = nw.front();
    q.push(f);
    dis[f.first][f.second] = 0;
    nw.pop();
  }
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    int x = u.first, y = u.second;
    if (dis[x][y] == k) continue;
    for (int i = 0; i < 4; i++) {
      int xx = x + fx[i], yy = y + fy[i];
      if (!valid(xx, yy)) continue;
      if (dis[xx][yy] != oo) continue;
      if (g[xx][yy] == '#') continue;
      dis[xx][yy] = dis[x][y] + 1;
      q.push({xx, yy});
    }
    if (fix[x][y] == oo) {
      Q.push({x, y});
      fix[x][y] = 0;
    }
    if (x == 0 || y == 0 || x == n - 1 || y == m - 1) return true;
  }
  while (!Q.empty()) {
    pair<int, int> u = Q.front();
    Q.pop();
    int x = u.first, y = u.second;
    g[x][y] = '.';
    nw.push({x, y});
    if (fix[x][y] == k) continue;
    for (int i = 0; i < 4; i++) {
      int xx = x + fx[i], yy = y + fy[i];
      if (!valid(xx, yy)) continue;
      if (fix[xx][yy] != oo) continue;
      fix[xx][yy] = fix[x][y] + 1;
      Q.push({xx, yy});
    }
  }
  return false;
}
void init() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dis[i][j] = oo;
      fix[i][j] = oo;
    }
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%s", g[i]);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (g[i][j] == 'S') {
        nw.push({i, j});
      }
    }
  }
  init();
  int ret = 1;
  while (!process()) {
    ++ret;
  }
  cout << ret << endl;
  return 0;
}
