#include <bits/stdc++.h>
using namespace std;
const int MAXN = 805;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int H, W, K, ans;
struct State {
  bool brd[MAXN][MAXN];
  bool vis[MAXN][MAXN];
  int cx, cy, cd, steps;
  bool arrive() {
    if (this->steps > K) {
      ans = K;
      return true;
    } else if (cx == 0 || cx == H - 1 || cy == 0 || cy == W - 1) {
      ans = this->steps;
      return true;
    }
    return false;
  }
} init;
queue<State> Q;
int main() {
  cin >> H >> W >> K;
  char c;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c;
      if (c == '.') {
        init.brd[i][j] = true;
      } else if (c == '#') {
        init.brd[i][j] = false;
      } else {
        init.brd[i][j] = true;
        init.cx = i;
        init.cy = j;
        init.vis[i][j] = true;
      }
    }
  }
  init.cd = 4;
  Q.push(init);
  while (!Q.empty()) {
    State state = Q.front();
    Q.pop();
    if (state.arrive()) break;
    if (state.cd == 4) {
      for (int i = 0; i < 4; i++) {
        state.cx += dx[i];
        state.cy += dy[i];
        if (!state.vis[state.cx][state.cy]) {
          if (state.brd[state.cx][state.cy]) {
            state.vis[state.cx][state.cy] = true;
            state.steps++;
            Q.push(state);
            state.vis[state.cx][state.cy] = false;
            state.steps--;
            state.cx -= dx[i];
            state.cy -= dy[i];
          } else {
            state.brd[state.cx][state.cy] = true;
            state.cd = i;
            state.steps++;
            state.cx -= dx[i];
            state.cy -= dy[i];
            Q.push(state);
            state.brd[state.cx][state.cy] = false;
            state.cd = 4;
            state.steps--;
          }
        }
      }
    } else {
      state.cx += dx[state.cd];
      state.cy += dy[state.cd];
      state.cd = 4;
      state.steps++;
      state.vis[state.cx][state.cy] = true;
      Q.push(state);
    }
  }
  cout << ans << '\n';
  return 0;
}
