#include <bits/stdc++.h>
using namespace std;
int n, m, k;
bool vis[805][805];
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
char ch[805][805];
struct data {
  int a, b, c;
  data(int x, int y, int z) {
    a = x;
    b = y;
    c = z;
  }
};
queue<data> q;
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> ch[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (ch[i][j] == 'S') q.push(data(i, j, 0)), vis[i][j] = 1;
  while (!q.empty()) {
    data f = q.front();
    q.pop();
    for (int d = 0; d < 4; d++) {
      int x = f.a + dx[d];
      int y = f.b + dy[d];
      if (x >= 1 && x <= n && y >= 1 && y <= m)
        if (!vis[x][y]) {
          if (ch[x][y] == '.')
            q.push(data(x, y, f.c + 1));
          else
            q.push(data(x, y, f.c + 2));
          if (x == 1 || y == 1 || x == n || y == n) {
            cout << q.front().c << endl;
            return 0;
          }
        }
    }
  }
}
