#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
	int h, w;
	cin >> h >> w;
	vector<vector<int>>grid(h+2, vector<int>(w+2,0));
	queue<pair<int, int>>black;
	for (int i = 1; i < h + 1; i++) {
		for (int j = 1; j < w + 1; j++) {
			char x;
			cin >> x;
			if (x == '.')grid.at(i).at(j) = 1e9;
			else black.push({ i, j });
		}
	}
	while (black.size() >= 1) {
		int x = black.front().first;
		int y = black.front().second;
		if (grid.at(x - 1).at(y) > grid.at(x).at(y) + 1) {
			grid.at(x - 1).at(y) = grid.at(x).at(y) + 1;
			black.push({ x - 1,y });
		}
		if (grid.at(x + 1).at(y) > grid.at(x).at(y) + 1) {
			grid.at(x + 1).at(y) = grid.at(x).at(y) + 1;
			black.push({ x + 1,y });
		}
		if (grid.at(x).at(y - 1) > grid.at(x).at(y) + 1) {
			grid.at(x).at(y - 1) = grid.at(x).at(y) + 1;
			black.push({ x,y - 1 });
		}
		if (grid.at(x).at(y + 1) > grid.at(x).at(y) + 1) {
			grid.at(x).at(y + 1) = grid.at(x).at(y) + 1;
			black.push({ x,y + 1 });
		}
		black.pop();
	}
	int ans = 0;
	for (int i = 1; i < h + 1; i++) {
		for (int j = 1; j < w + 1; j++) {
			ans = max(ans, grid.at(i).at(j));
		}
	}
	cout << ans << endl;
}
#include <bits/stdc++.h>
#include <string>
using namespace std;
typedef long long ll;

int main(){
    std::vector<string> d(3);
    for(ll i=0;i<3;++i){
        cin>>d[i];
    }
    ll a=0,b=0,c=0;
    char now='a';
    while(1){
        switch(now){
            case 'a':
                if(a>=d[0].length()){
                    cout<<char(now+'A'-'a');
                    return 0;
                }
                now=d[0][a];
                a++;
            break;
            case 'b':
                if(b>=d[1].length()){
                    cout<<char(now+'A'-'a');
                    return 0;
                }
                now=d[1][b];
                b++;
            break;
            case 'c':
                if(c>=d[2].length()){
                    cout<<char(now+'A'-'a');
                    return 0;
                }
                now=d[2][c];
                c++;
            break;
        }
    }
    return 0;
}