#include <bits/stdc++.h>
using namespace std;
struct node {
  int a, b, c;
  node() {}
  node(int e, int f, int g) {
    a = e;
    b = f;
    c = g;
  }
};
int ce(int a, int b) { return (a % b == 0 ? a / b : a / b + 1); }
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
queue<node> q;
bool vis[805][805];
char c[805][805];
int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  for (int(i) = 0; i < (int)(N); i++) cin >> c[i];
  int X, Y;
  for (int(i) = 0; i < (int)(N); i++)
    for (int(j) = 0; j < (int)(M); j++)
      if (c[i][j] == 'S') X = i, Y = j;
  if (X == 0 || Y == 0 || X == N - 1 || Y == M - 1) {
    cout << 0 << endl;
    return 0;
  }
  bool ok = 1;
  for (int(d) = 0; d < (int)(4); d++) {
    int Xn = X + dx[d];
    int Yn = Y + dy[d];
    if (c[Xn][Yn] != '#') {
      ok = 0;
      break;
    }
  }
  int dis = 0;
  q.push(node(X, Y, 0));
  while (!q.empty()) {
    int x = q.front().a;
    int y = q.front().b;
    int z = q.front().c;
    q.pop();
    if (x == 0 || y == 0 || x == N - 1 || y == M - 1) {
      dis = z;
      break;
    }
    if (x < 0 || y < 0 || x >= N || y >= M) continue;
    if (vis[x][y]) continue;
    vis[x][y] = 1;
    for (int(d) = 0; d < (int)(4); d++) {
      int xn = x + dx[d];
      int yn = y + dy[d];
      q.push(node(xn, yn, z + 1));
    }
  }
  if (ok)
    cout << ce(dis, K) + 1 << endl;
  else
    cout << ce(dis, K) << endl;
  return 0;
}
