#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char a[801][801];
bool visited[801][801];
pair<int, int> start;
vector<pair<long long, pair<int, int> > > adj[801][801];
long long dist[801][801];
long long mini = 1600;
void dijkstra() {
  priority_queue<pair<long long, pair<int, int> >,
                 vector<pair<long long, pair<int, int> > >,
                 greater<pair<long long, pair<int, int> > > >
      vertices;
  vertices.push(make_pair(0, start));
  while (!vertices.empty()) {
    pair<int, int> x = (vertices.top()).second;
    vertices.pop();
    if (!visited[x.first][x.second]) {
      for (auto it = adj[x.first][x.second].begin();
           it != adj[x.first][x.second].end(); it++) {
        long long w1;
        w1 = (*it).first;
        pair<int, int> other = (*it).second;
        if (dist[x.first][x.second] + w1 < dist[other.first][other.second]) {
          dist[other.first][other.second] = dist[x.first][x.second] + w1;
          vertices.push(make_pair(dist[other.first][other.second], other));
        }
      }
    }
    visited[x.first][x.second] = 1;
  }
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        start.first = i;
        start.second = j;
      }
      if (a[i][j] == 'S' || a[i][j] == '.') {
        if (a[i + 1][j] == '.') {
          adj[i][j].push_back(make_pair(1, make_pair(i + 1, j)));
        }
        if (a[i][j + 1] == '.') {
          adj[i][j].push_back(make_pair(1, make_pair(i, j + 1)));
        }
        if (a[i - 1][j] == '.') {
          adj[i][j].push_back(make_pair(1, make_pair(i - 1, j)));
        }
        if (a[i][j - 1] == '.') {
          adj[i][j].push_back(make_pair(1, make_pair(i, j - 1)));
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dist[i][j] = 1e9;
    }
  }
  dist[start.first][start.second] = 0;
  dijkstra();
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int x = i;
      int y = j;
      if (dist[i][j] <= k) {
        if (mini > x) {
          mini = x;
        }
        if (mini > h - 1 - x) {
          mini = h - 1 - x;
        }
        if (mini > y) {
          mini = y;
        }
        if (mini > w - 1 - y) {
          mini = w - 1 - y;
        }
      }
    }
  }
  if (mini == 0) {
    cout << 1 << '\n';
    return 0;
  }
  int ans = 1;
  ans += (mini / k);
  mini = (mini % k);
  if (mini > 0) {
    ans++;
  }
  cout << ans << '\n';
  return 0;
}
