#include <bits/stdc++.h>
const double PI = acos(-1.0);
const int DIR = 4;
const int X[] = {1, 0, -1, 0};
const int Y[] = {0, 1, 0, -1};
const int N = 805;
using namespace std;
int n, m, k, dp[N][N], nw[N][N];
string s[N];
bool good(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) cin >> s[i];
  memset(dp, -1, sizeof(dp));
  memset(nw, -1, sizeof(nw));
  int x1 = -1, qewr1234 = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S') {
        x1 = i, qewr1234 = j;
        s[i][j] = '.';
      }
    }
  }
  dp[x1][qewr1234] = 0;
  int d = 0;
  while (true) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (dp[i][j] == -1) continue;
        for (int dir = 0; dir < DIR; dir++) {
          int tx = i + X[dir];
          int ty = j + Y[dir];
          if (good(tx, ty) && dp[i][j] >= bool(s[tx][ty] == '#')) {
            int ndp =
                dp[i][j] - bool(s[tx][ty] == '#') + k * bool((d + 1) % k == 0);
            nw[tx][ty] = max(nw[tx][ty], ndp);
          }
        }
        nw[i][j] = max(nw[i][j], dp[i][j] + bool((d + 1) % k == 0) * k);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if ((i == 0 || j == 0) && dp[i][j] != -1) {
          cout << (d + k - 1) / k << endl;
          return 0;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        dp[i][j] = nw[i][j];
        nw[i][j] = -1;
      }
    }
    d++;
  }
  return 0;
}
