#include <bits/stdc++.h>
using namespace std;
int n, m, k, step = 0;
pair<int, int> s, bas;
vector<vector<char> > vec;
vector<vector<int> > ans;
queue<pair<int, int> > q;
vector<vector<int> > visit;
void dene();
void kir();
void kir() {
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    int cx = x, cy = y;
    int i = 0;
    while (cx > 0 and k > i) {
      cx--;
      if (vec[cx][y] == '#') {
        vec[cx][y] = '.';
        i++;
      }
    }
    i = 0;
    cx = x;
    while (n - 1 > cx and k > i) {
      cx++;
      if (vec[cx][y] == '#') {
        vec[cx][y] = '.';
        i++;
      }
    }
    i = 0;
    cy = y;
    while (cy > 0 and k > i) {
      cy--;
      if (vec[x][cy] == '#') {
        vec[x][cy] = '.';
        i++;
      }
    }
    i = 0;
    cy = y;
    while (n - 1 > cy and k > i) {
      cy++;
      if (vec[x][cy] == '#') {
        vec[x][cy] = '.';
        i++;
      }
    }
    s = {x, y};
    dene();
  }
}
void dene() {
  int x = s.first;
  int y = s.second;
  visit[x][y] = 1;
  bool sol = true, sag = true, yukari = true, asagi = true;
  ++step;
  bool islem = false;
  for (int i = 1; k >= i; i++) {
    if (x + i < n and vec[x + i][y] == '.' and sag and !visit[x + i][y]) {
      ans[x + i][y] = step;
      q.push({x + i, y});
      visit[x + i][y] = 1;
      islem = true;
    }
    if (y + i < m and vec[x][y + i] == '.' and yukari and !visit[x][y + i]) {
      ans[x][y + i] = step;
      q.push({x, y + i});
      visit[x][y + i] = 1;
      islem = true;
    }
    if (x - i >= 0 and vec[x - i][y] == '.' and sol and !visit[x - i][y]) {
      ans[x - i][y] = step;
      q.push({x - i, y});
      visit[x - i][y] = 1;
      islem = true;
    }
    if (y - i >= 0 and vec[x][y - i] == '.' and asagi and !visit[x][y - i]) {
      ans[x][y - i] = step;
      q.push({x, y - i});
      visit[x][y - i] = 1;
      islem = true;
    }
    if (x + i < n and vec[x + i][y] == '#') {
      sag = false;
    }
    if (x - i >= 0 and vec[x - i][y] == '#') {
      sol = false;
    }
    if (y + i < n and vec[x][y + i] == '#') {
      yukari = false;
    }
    if (y - i > 0 and vec[x][y - i] == '#') {
      asagi = false;
    }
  }
  if (islem) {
    kir();
  } else if (s == bas) {
    q.push({x, y});
    kir();
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m >> k;
  vec.resize(n, vector<char>(m));
  ans.resize(n, vector<int>(m, 100000000));
  visit.resize(n, vector<int>(m, 0));
  for (int i = 0; n > i; i++) {
    for (int j = 0; m > j; j++) {
      cin >> vec[i][j];
      if (vec[i][j] == 'S') {
        s = {i, j};
        ans[i][j] = 0;
      }
    }
  }
  bas = s;
  dene();
  int mini = 100000000;
  for (int i = 0; n > i; i++) {
    mini = min(mini, ans[i][0]);
    mini = min(mini, ans[i][m - 1]);
  }
  for (int i = 0; m > i; i++) {
    mini = min(mini, ans[0][i]);
    mini = min(mini, ans[n - 1][i]);
  }
  cout << mini;
}
