public int longestSequence(int[][]A) {
		int m=A.length, n=A[0].length;
		int[][]dp=new int[m][n];
		int rel=0;
		for (int i=m-1;i>=0;--i)
			for (int j=n-1;j>=0;--j) {
				dp[i][j]=1;
				if (i+1<m&&Math.abs(A[i][j]-A[i+1][j])==1) dp[i][j]=Math.max(dp[i][j], dp[i+1][j]+1);
				if (j+1<n&&Math.abs(A[i][j]-A[i][j+1])==1) dp[i][j]=Math.max(dp[i][j], dp[i][j+1]+1);
				rel=Math.max(rel, dp[i][j]);
			}
		return rel;
	}