#include <bits/stdc++.h>
using namespace std;
char a[1000][1000];
int n, m, k;
queue<pair<pair<int, int>, int> > q;
int dist[1000][1000];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int main() {
  cin >> n >> m >> k;
  int sx, sy;
  for (int i = 1; i <= n; i++) {
    scanf("%s", a[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  q.push(make_pair(make_pair(sx, sy), 0));
  memset(dist, 0x3f, sizeof(dist));
  dist[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().first.first;
    int y = q.front().first.second;
    int stp = q.front().second;
    q.pop();
    if (stp > k) continue;
    if ((x == 1 || y == 1 || x == n || y == m) && stp <= k) {
      cout << 1 << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (a[nx][ny] == '.' && dist[nx][ny] >= 1e9) {
        q.push(make_pair(make_pair(nx, ny), stp + 1));
        dist[nx][ny] = stp + 1;
      }
    }
  }
  cout << 2 << endl;
  return 0;
}
