#include <bits/stdc++.h>
using namespace std;
const int MAXN = 800 + 50;
char f[MAXN + 1][MAXN + 1];
int d[MAXN + 1][MAXN + 1], n, m, k,
    mv[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}, calc,
    used[MAXN + 1][MAXN + 1], dist[MAXN + 1][MAXN + 1];
bool valid(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m; }
bool win(int x, int y) { return (x == 1) || (x == n) || (y == 1) || (y == m); }
void go(vector<pair<int, int> >& v) {
  deque<pair<int, int> > q;
  vector<pair<int, int> > was;
  for (auto it : v) {
    q.push_back(it);
    dist[it.first][it.second] = 0;
    was.push_back({it.first, it.second});
  }
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    if (dist[x][y] == k) break;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int dx = x + mv[i][0];
      int dy = y + mv[i][1];
      if (valid(dx, dy) && d[dx][dy] == -1 && dist[dx][dy] == -1) {
        dist[dx][dy] = dist[x][y];
        was.push_back({dx, dy});
        int ok = 0;
        if (f[dx][dy] == '#') f[dx][dy] = '.', ok = 1;
        dist[dx][dy] += ok;
        if (!ok)
          q.push_front({dx, dy});
        else
          q.push_back({dx, dy});
      }
    }
  }
  for (auto it : was) dist[it.first][it.second] = -1;
}
int main() {
  cin >> n >> m >> k;
  int a = -1, b = -1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> f[i][j];
      if (f[i][j] == 'S') a = i, b = j;
      d[i][j] = -1;
    }
  }
  memset(dist, -1, sizeof(dist));
  queue<pair<int, int> > q;
  q.push({a, b});
  d[a][b] = 0;
  int cur = k, ans = 1;
  vector<pair<int, int> > was;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    if (win(x, y)) {
      cout << ans << "\n";
      return 0;
    }
    was.push_back({x, y});
    q.pop();
    if (d[x][y] == cur) {
      go(was);
      cur += k;
      ans++;
      for (auto it : was) q.push(it);
      was.clear();
    }
    for (int i = 0; i < 4; i++) {
      int dx = x + mv[i][0];
      int dy = y + mv[i][1];
      if (valid(dx, dy) && d[dx][dy] == -1 && f[dx][dy] != '#') {
        d[dx][dy] = d[x][y] + 1;
        if (win(dx, dy)) {
          cout << ans << "\n";
          return 0;
        }
        q.push({dx, dy});
      }
    }
    if (q.empty()) {
      go(was);
      cur += k;
      ans++;
      for (auto it : was) q.push(it);
      was.clear();
    }
  }
  cout << ans << "\n";
  return 0;
}
