#include <bits/stdc++.h>
int H, W, K;
std::string A[812];
int field[812][812], sx, sy, ans = 114514810;
int sum(int i, int j, int h, int w) {
  return field[i + h][j + w] - field[i][j + w] + field[i][j] - field[i + h][j];
}
int main() {
  std::cin >> H >> W >> K;
  for (long long int i = 0; i < H; ++i) {
    std::cin >> A[i];
  }
  for (long long int i = 0; i < H; ++i) {
    for (long long int j = 0; j < W; ++j) {
      field[i + 1][j + 1] = (A[i][j] == '.' || A[i][j] == 'S' ? 1 : 0);
      if (A[i][j] == 'S') {
        sx = i, sy = j;
      }
    }
  }
  for (long long int i = 0; i < H + 1; ++i) {
    for (long long int j = 0; j < W; ++j) {
      field[i][j + 1] += field[i][j];
    }
  }
  for (long long int i = 0; i < H; ++i) {
    for (long long int j = 0; j < W + 1; ++j) {
      field[i + 1][j] += field[i][j];
    }
  }
  for (long long int i = 0; i < H; ++i) {
    ans = std::min<long long int>(ans, (sx - i) + (sy - 0));
  }
  return 0;
}
