#include <bits/stdc++.h>
using namespace std;
int DX[4] = {-1, 0, 0, 1};
int DY[4] = {0, -1, 1, 0};
string a[1000];
queue<pair<int, int>> q;
int d[1000][1000];
bool isfree(int m, int n, int x, int y) {
  return (x >= 0 && x < m && y >= 0 && y < n && a[x][y] == '.');
}
bool isexit(int m, int n, int x, int y) {
  return (x == 0 || y == 0 || x == m - 1 || y == n - 1);
}
int minstep(int m, int n, int k, int x, int y) {
  if (isexit(m, n, x, y)) return 0;
  int ans = (x - 1) / k + 1;
  ans = min(ans, (y - 1) / k + 1);
  ans = min(ans, (m - x - 1) / k + 1);
  ans = min(ans, (n - y - 1) / k + 1);
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  int m, n, k;
  while (cin >> m >> n >> k) {
    int sx, sy, x, y, newx, newy;
    while (!q.empty()) q.pop();
    for (int i = (0), _b = (m - 1); i <= _b; i++) cin >> a[i];
    for (int i = (0), _b = (m - 1); i <= _b; i++)
      for (int j = (0), _b = (n - 1); j <= _b; j++)
        if (a[i][j] == 'S') {
          sx = i;
          sy = j;
          d[i][j] = 0;
          q.push(make_pair(i, j));
        } else
          d[i][j] = -1;
    while (!q.empty()) {
      pair<int, int> e = q.front();
      q.pop();
      x = e.first;
      y = e.second;
      if (d[x][y] == k) continue;
      for (int dir = (0), _b = (3); dir <= _b; dir++) {
        newx = x + DX[dir];
        newy = y + DY[dir];
        if (isfree(m, n, newx, newy) && d[newx][newy] == -1) {
          d[newx][newy] = d[x][y] + 1;
          q.push(make_pair(newx, newy));
        }
      }
    }
    int res = m * n;
    for (int i = (0), _b = (m - 1); i <= _b; i++)
      for (int j = (0), _b = (n - 1); j <= _b; j++)
        if (d[i][j] != -1) {
          res = min(res, 1 + minstep(m, n, k, i, j));
        }
    cout << res << endl;
  }
  return 0;
}
