//bfs 1000100010不会超时
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int n=1e3+11;
int a,b,c,h[n][n][12],k[4][2]={0,1,0,-1,1,0,-1,0};
char t[n][n];
struct s
{int x,y,z,w;
};
signed main()
{ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);cin>>a>>b>>c;for(int i=1;i<=a;i++){for(int j=1;j<=b;j++){cin>>t[i][j];}}queue<s>q;q.push({1,1,1,0});h[1][1][1]=1;while(!q.empty()){s m=q.front();q.pop();if(m.x==a&&m.y==b){cout<<m.w<<endl;return 0;}int xx,yy;for(int i=0;i<4;i++){xx=m.x+k[i][0];yy=m.y+k[i][1];if(xx>=1&&xx<=a&&yy>=1&&yy<=b){if(m.z==c){if(h[xx][yy][1]==0&&t[xx][yy]=='B'&&t[m.x][m.y]=='A'){h[xx][yy][1]=1;q.push({xx,yy,1,m.w+1});}if(h[xx][yy][1]==0&&t[xx][yy]=='A'&&t[m.x][m.y]=='B'){h[xx][yy][1]=1;q.push({xx,yy,1,m.w+1});}}else{if(t[xx][yy]==t[m.x][m.y]&&h[xx][yy][m.z+1]==0){h[xx][yy][m.z+1]=1;q.push({xx,yy,m.z+1,m.w+1});}}}}}cout<<"-1"<<endl;
}