```cpp
#include<bits/stdc++.h>
using namespace std;
int p,r,z,c,sum[2600][2600],a[2600][2600],n,m,ans,pd[2600][2600],num[2600][2600],ji;
int check(int e,int t){
for(int i=min(z,p);i<=max(z,p);i++){
if(sum[i][max(c,r)]-sum[i][min(c,r)-1]>1){
return 0;
}
}
return 1;
}
struct node{
int x;
int y;
int stemp;
}w;
node u;
queue<node> q;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
sum[i][j]=sum[i][j-1]+a[i][j];
if(a[i][j]==1){
u.x=i;
u.y=j;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]==1){
z=i,c=j;
q.push((node){i,j,1});
while(q.size()){
w=q.front();
q.pop();
p=w.x-1;
r=w.y+1;
if(p<=0||r>m){
break;
}
if(check(p,r)){
q.push((node){p,r,w.stemp+1});
}
ans=max(ans,w.stemp);
}
q.push((node){i,j,1});
while(q.size()){
w=q.front();
q.pop();
p=w.x+1;
r=w.y+1;
if(p>n||r>m){
break;
}
if(check(p,r)){
q.push((node){p,r,w.stemp+1});
}
ans=max(ans,w.stemp);
}
q.push((node){i,j,1});
while(q.size()){
w=q.front();
q.pop();
p=w.x+1;
r=w.y-1;
if(p>n||r<=0){
break;
}
if(check(p,r)){
q.push((node){p,r,w.stemp+1});
}
ans=max(ans,w.stemp);
}
q.push((node){i,j,1});
while(q.size()){
w=q.front();
q.pop();
p=w.x-1;
r=w.y-1;
if(p<=0||r<=0){
break;
}
if(check(p,r)){
q.push((node){p,r,w.stemp+1});
}
ans=max(ans,w.stemp);
}
}
if(i==u.x&&j==u.y){
break;
}
}
}
cout<<ans;
}
```
by 陈彦锟 @ 2019-10-10 20:52:41