#include <bits/stdc++.h>
using namespace std;
deque<pair<pair<int, int>, int> > pos;
bool vis[805][805];
bool Bvis[805][805];
int N, M, K;
char kotak[805][805];
int x1[4] = {0, 1, 0, -1};
int y2[4] = {1, 0, -1, 0};
vector<pair<int, int> > tmp;
bool bfs() {
  while (!pos.empty()) pos.pop_front();
  for (int z = 0; z < tmp.size(); z++) {
    pos.push_back(pair<pair<int, int>, int>(tmp[z], K));
    vis[tmp[z].first][tmp[z].second] = 1;
  }
  tmp.clear();
  while (!pos.empty()) {
    int nowx = pos.front().first.second, nowy = pos.front().first.first,
        nowcost = pos.front().second;
    pos.pop_front();
    if (nowx == 1 || nowy == 1 || nowx == M || nowy == N) return 1;
    bool done = 0;
    for (int z = 0; z < 4; z++) {
      int newy = nowy + y2[z], newx = nowx + x1[z];
      if (newy < 1 || newx < 1 || newy > N || newx > M) continue;
      if (kotak[newy][newx] == '#') continue;
      if (vis[newy][newx]) continue;
      vis[newy][newx] = 1;
      pos.push_back(
          pair<pair<int, int>, int>(pair<int, int>(newy, newx), nowcost - 1));
      done = 1;
    }
    if (!done) {
      tmp.push_back(pair<int, int>(nowy, nowx));
    }
  }
  return 0;
}
void createpath() {
  while (!pos.empty()) pos.pop_front();
  for (int z = 0; z < tmp.size(); z++) {
    pos.push_back(pair<pair<int, int>, int>(tmp[z], K));
    Bvis[tmp[z].first][tmp[z].second] = 1;
  }
  queue<pair<int, int> > STMP;
  while (!pos.empty()) {
    int nowx = pos.front().first.second, nowy = pos.front().first.first,
        nowcost = pos.front().second;
    pos.pop_front();
    STMP.push(pair<int, int>(nowy, nowx));
    if (nowcost == 0) continue;
    for (int z = 0; z < 4; z++) {
      int newy = nowy + y2[z], newx = nowx + x1[z];
      if (newy < 1 || newx < 1 || newy > N || newx > M) continue;
      if (Bvis[newy][newx]) continue;
      Bvis[newy][newx] = 1;
      pos.push_back(pair<pair<int, int>, int>(
          pair<int, int>(newy, newx), nowcost - (kotak[newy][newx] == '#')));
    }
  }
  while (!STMP.empty()) {
    kotak[STMP.front().first][STMP.front().second] = '.';
    STMP.pop();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M >> K;
  for (int a = 1; a <= N; a++) {
    for (int b = 1; b <= M; b++) {
      cin >> kotak[a][b];
      if (kotak[a][b] == 'S') {
        tmp.push_back(pair<int, int>(a, b));
      }
    }
  }
  int cnt = 1;
  while (!bfs()) {
    createpath();
    cnt++;
  }
  cout << cnt << endl;
  return 0;
}
