#include <bits/stdc++.h>
using namespace std;
void dfs(int, int, int);
int H, L, step;
int start[2], best = 0xFFFFFFF, used[800][800];
char a[800][801];
int main() {
  cin >> H >> L >> step;
  for (int i = 0; i != H; i++) {
    cin >> a[i];
    for (int j = 0; j != L; j++) {
      if (a[i][j] == 'S') {
        start[0] = j;
        start[1] = i;
      }
    }
  }
  L--;
  H--;
  dfs(start[0], start[1], 0);
  if (best % step) {
    cout << best / step + 2 << endl;
  } else {
    cout << best / step + 1 << endl;
  }
  return 0;
}
void dfs(int x, int y, int depth) {
  if (a[x][y] == '#' or used[x][y] or depth == step or x == -1 or y == -1 or
      x == H or y == L) {
    return;
  }
  used[x][y] = 1;
  int max = x;
  if (y < max) max = y;
  if (H - y < max) max = H - y;
  if (L - x < max) max = L - x;
  if (max < best) best = max;
  dfs(x + 1, y, depth + 1);
  dfs(x - 1, y, depth + 1);
  dfs(x, y + 1, depth + 1);
  dfs(x, y - 1, depth + 1);
}
