P1681
和上一道题差不多,要注意一些细节
#include<iostream>
#include<utility>
using namespace std;
typedef long long ll;
#define fi(i,a,b) for(int i = a; i <= b; ++i)
#define fr(i,a,b) for(int i = a; i >= b; --i)
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define pb push_back
using pii = pair<int,int>;
//#define DEBUG
int matrix[1505][1505];
int dp[1505][1505];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef DEBUG
//freopen(D:\in.txt,r,stdin);
#endif
int n,m;
cin >> n >> m;
fi(i,1,n) fi(j,1,m) cin >> matrix[i][j];
fi(i,1,n) fi(j,1,m){
if(i == 1 || j == 1) {
dp[i][j] = 1;
}
else{
int s = min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]));
if(matrix[i][j] != matrix[i-1][j] && matrix[i][j] != matrix[i][j-1] && matrix[i][j] == matrix[i-1][j-1])
dp[i][j] = s + 1;
else dp[i][j] = 1;
}
// cout << dp[i][j] << endl;
}
int maxx = 0;
fi(i,1,n) fi(j,1,m) {
maxx = max(dp[i][j],maxx);
}
if(maxx == 1) maxx = 0;
cout << maxx << endl;
return 0;
}