#include <bits/stdc++.h>
using namespace std;
char a[801][801];
int n, m, k, mi;
void check(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m || a[i][j] != '.') return;
  a[i][j] = '1';
  int r[4];
  r[0] = i;
  r[1] = j;
  r[2] = n - i - 1;
  r[3] = m - j - 1;
  for (int i = 0; i < 4; ++i)
    if (mi > r[i]) mi = r[i];
  check(i + 1, j);
  check(i - 1, j);
  check(i, j + 1);
  check(i, j - 1);
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) cin >> a[i][j];
  mi = 801 + 10;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (a[i][j] == 'S') {
        a[i][j] = '.';
        check(i, j);
        int res = 1;
        if (mi % k != 0) ++res;
        res += mi / k;
        cout << res;
        return 0;
      }
  return 0;
}
