#include <bits/stdc++.h>
using namespace std;
void init() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int n, m, k;
char arr[805][805];
int fille[805][805];
int dr[] = {0, 1, 0, -1};
int dc[] = {1, 0, -1, 0};
int main() {
  init();
  cin >> n >> m >> k;
  int x, y;
  for (long long int(i) = (0); (i) < (n); ++(i)) {
    for (long long int(j) = (0); (j) < (m); ++(j)) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  for (long long int(i) = (0); (i) < (805); ++(i)) {
    for (long long int(j) = (0); (j) < (805); ++(j)) fille[i][j] = -1;
  }
  int count = 0;
  queue<pair<pair<int, int>, int>> q;
  q.push(make_pair(make_pair(x, y), k));
  arr[x][y] = '&';
  queue<pair<pair<int, int>, int>> lock;
  queue<pair<pair<int, int>, int>> akhir;
  int rem;
  int nemu = 0;
  int find = 0;
  while (true) {
    count++;
    while (q.empty() == false) {
      x = q.front().first.first;
      y = q.front().first.second;
      rem = q.front().second;
      q.pop();
      if (x == 0 || x == n - 1 || y == 0 || y == m - 1) {
        nemu = 1;
        break;
      }
      if (rem == 0) {
        lock.push(make_pair(make_pair(x, y), k));
        akhir.push(make_pair(make_pair(x, y), k));
        continue;
      }
      find = 0;
      for (long long int(i) = (0); (i) < (4); ++(i)) {
        if (x + dr[i] < 0 || x + dr[i] > n || y + dc[i] < 0 || y + dc[i] > m)
          continue;
        if (arr[x + dr[i]][y + dc[i]] == '.') {
          find++;
          arr[x + dr[i]][y + dc[i]] = '&';
          q.push(make_pair(make_pair(x + dr[i], y + dc[i]), rem - 1));
        }
      }
      if (find == 0) {
        lock.push(make_pair(make_pair(x, y), k));
        akhir.push(make_pair(make_pair(x, y), k));
        continue;
      }
    }
    if (nemu == 1) break;
    while (lock.empty() == false) {
      x = lock.front().first.first;
      y = lock.front().first.second;
      rem = lock.front().second;
      lock.pop();
      if (rem == 0) continue;
      for (long long int(i) = (0); (i) < (4); ++(i)) {
        if (x + dr[i] < 0 || x + dr[i] > n || y + dc[i] < 0 || y + dc[i] > m)
          continue;
        if (arr[x + dr[i]][y + dc[i]] == '#') {
          arr[x + dr[i]][y + dc[i]] = '.';
          lock.push(make_pair(make_pair(x + dr[i], y + dc[i]), rem - 1));
        }
      }
    }
    while (akhir.empty() == false) {
      q.push(akhir.front());
      akhir.pop();
    }
  }
  cout << count << '\n';
  return 0;
}
