#include <bits/stdc++.h>
using namespace std;
int n, m, k, d[1000][1000];
bool in(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m) return 0;
  return 1;
}
pair<int, int> s;
queue<pair<int, int> > q;
char a[1000][1000];
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
      if (i == 1 || j == 1 || i == n || j == m)
        q.push(make_pair(i, j));
      else
        d[i][j] = 500;
      if (a[i][j] == 'S') s = make_pair(i, j);
    }
  }
  while (int(q.size())) {
    pair<int, int> p = q.front();
    q.pop();
    if (a[p.first][p.second] == '#') d[p.first][p.second] += 1;
    for (int i = -1; i <= 1; ++i) {
      for (int j = -1; j <= 1; ++j) {
        if (abs(i + j) != 1) continue;
        if (!in(p.first + i, p.second + j)) continue;
        if (d[p.first + i][p.second + j] > d[p.first][p.second] + 1) {
          d[p.first + i][p.second + j] = d[p.first][p.second] + 1;
          q.push(make_pair(p.first + i, p.second + j));
        }
      }
    }
  }
  cout << d[s.first][s.second];
  return 0;
}
