#include <bits/stdc++.h>
using namespace std;
const int MAX = 1111;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
int n, m, k;
string s[MAX];
bool was[MAX][MAX];
vector<pair<int, int> > cur, nxt;
bool good(int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m && !was[x][y];
}
void bfs(int x, int y) {
  int mn = k;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i][j] != '#' && abs(i - x) + abs(j - y) <= k) {
        cur.push_back(make_pair(i, j));
        was[i][j] = true;
        if (i == 0 || i == n - 1 || j == 0 || j == m - 1)
          mn = min(mn, abs(i - x) + abs(j - y));
      }
    }
  }
  if (mn < k) {
    cout << 1;
    return;
  }
  int cc = 0;
  while (!cur.empty()) {
    for (int i = 0; i < (int)cur.size(); ++i) {
      x = cur[i].first;
      y = cur[i].second;
      if (x == 0 || x == n - 1 || y == 0 || y == m - 1) {
        cout << 1 + cc / k + (cc % k != 0);
        return;
      }
      for (int j = 0; j < 4; ++j) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        if (good(nx, ny)) {
          was[nx][ny] = true;
          nxt.push_back(make_pair(nx, ny));
        }
      }
    }
    cur = nxt;
    nxt.clear();
    ++cc;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  int x, y;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == 'S') x = i, y = j;
    }
  }
  bfs(x, y);
  return 0;
}
