#include<iostream>
using namespace std;
int n, m, A[100][100];
int visited[100][100];
int x, y;
int rs[4] = {-1, 0, 1, 0};
int cs[4] = {0, 1, 0, -1};
int result;
int dau;
int cuoi;
int dau2, cuoi2;
int queue[10000];
int queue2[10000];
void bom(int x, int y){
visited[x][y] = true;
int c, d;
while(dau!=cuoi && dau2!=cuoi2){
c = queue[dau];
dau++;
d = queue2[dau2];
//cout<<c<<" "<<d<<endl;
dau2++;
for(int i=0; i<4; i++){
int new_r = c + rs[i];
int new_c = d + cs[i];
if(new_r>=0 && new_r<n && new_c>=0 && new_c<m && (visited[new_r][new_c]==false) && A[new_r][new_c]>0){
A[new_r][new_c] = A[c][d]+1;
queue[cuoi] = new_r;
cuoi++;
queue2[cuoi2] = new_c;
//cout<<new_r<<" "<<new_c<<endl;
cuoi2++;
visited[new_r][new_c]=visited[c][d] + 1;
}
}
}
}
int main(){
int t;
cin>>t;
for(int tc=1; tc<=t; tc++){
cin>>m>>n;
int max=0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>A[i][j];
visited[i][j]=0;
}
}
dau=0;
cuoi=0;
dau2 = 0;
cuoi2 = 0;
cuoi++;
cuoi2++;
cin>>x>>y;
queue[dau]=y-1;
queue2[dau2]=x-1;
result=0;
//cout<<x<<" "<<y<<endl;
bom(y-1,x-1);
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(A[i][j]>max) max = A[i][j];
}
}
//for(int i=0; i<n; i++){
// for(int j=0; j<m; j++){
// cout << visited[i][j] << " ";
// }
// cout << endl;
//}
cout<<max<<endl;
}
return 0;
}
--------------------------------------------------------------------------------------matranke
#include<iostream>
using namespace std;
int n, u, A[100];
int ke[105][105];
int visited[105];
int dau;
int cuoi;
int queue[10000];
void bom(int x){
visited[x] = true;
int c;
A[queue[dau]] = 1;
while(dau!=cuoi){
c = queue[dau];
dau++;
//cout<<c<<" "<<d<<endl;
for(int i=1; i<=100; i++){
if(visited[i]==false && ke[c][i]==1){
A[i] = A[c]+1;
queue[cuoi] = i;
cuoi++;
visited[i]=visited[c] + 1;
}
}
}
}
int main(){
for(int tc=1; tc<=10; tc++){
cin>>n>>u;
int a,b;
for(int i=1; i<=100; i++){
for(int j=1; j<=100; j++) ke[i][j]=0;
}
for(int i=1; i<=n/2; i++){
cin>>a>>b;
ke[a][b] = 1;
}
for(int i=1; i<=100; i++) visited[i]=0;
dau=0;
cuoi=0;
cuoi++;
queue[dau]=u;
for(int i=0; i<=100; i++){
A[i] = 0;
}
bom(u);
int max = 0;
int result = 0;
int check=0;
for(int i=0; i<=100; i++){
if(A[i]>=max){
max = A[i];
result=i;
}
//cout<<A[i]<<" ";
}
cout<<"#"<<tc<<" ";
cout<<result<<endl;
}
return 0;
}