#include <bits/stdc++.h>
using namespace std;
int n, m, k, b1, b2, vis[805][805], mn = 1000000000;
char A[805][805];
int dx[5] = {1, 0, -1, 0};
int dy[5] = {0, -1, 0, 1};
queue<pair<int, pair<int, pair<int, pair<int, int> > > > > q;
void bfs() {
  q.push(make_pair(1, make_pair(b1, make_pair(b2, make_pair(k, 0)))));
  while (!q.empty()) {
    pair<int, pair<int, pair<int, pair<int, int> > > > temp = q.front();
    q.pop();
    int cst = temp.first;
    int seh1 = temp.second.first;
    int seh2 = temp.second.second.first;
    int yuru = temp.second.second.second.first;
    int tos = temp.second.second.second.second;
    if (vis[seh1][seh2] <= cst) continue;
    vis[seh1][seh2] = cst;
    if (seh1 == 1 || seh2 == 1 || seh1 == n || seh2 == m) {
      mn = min(mn, cst);
      continue;
    }
    for (int i = 0; i <= 3; i++) {
      int gox = seh1 + dx[i];
      int goy = seh2 + dy[i];
      if (A[gox][goy] == '#') {
        if (tos == 0 || yuru == 0) {
          q.push(make_pair(
              (cst + 1),
              make_pair(gox,
                        make_pair(goy, make_pair(yuru + k - 1, tos + k - 1)))));
        } else {
          q.push(make_pair(
              cst,
              make_pair(gox, make_pair(goy, make_pair(yuru - 1, tos - 1)))));
        }
      } else {
        if (yuru == 0) {
          q.push(make_pair(
              (cst + 1),
              make_pair(gox, make_pair(goy, make_pair(k - 1, tos + k)))));
        } else {
          q.push(make_pair(
              cst, make_pair(gox, make_pair(goy, make_pair(yuru - 1, tos)))));
        }
      }
    }
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf(" %c", &A[i][j]);
      if (A[i][j] == 'S') {
        b1 = i;
        b2 = j;
      }
      vis[i][j] = 1000000000;
    }
  }
  bfs();
  printf("%d\n", mn);
  return 0;
}
