Constraint for 2D Array so that no two adjacent elements are same

Write a constraint for a NXN 2D Array such that no two adjacent elements are same. Also, the difference between adjacent elements should be between +4 and -4. For a 2D array, you have to take care of adjacent element between consecutive rows and columns as well.

//Create 4x4 matrix which each adjacent element has unique value
class a2d_array;
rand bit signed [5:0] subm[4][4];

constraint array_c{
foreach(subm[i,j]){
subm[i].sum(item) with (int’(item == subm[i][item.index])) == 1;//Row is unique
}
foreach(subm[i,j]){
subm[j].sum(item) with (int’(item == subm[item.index][j])) == 1;
}
foreach(subm[i,j]){
if(i<3 && j < 3){
(subm[i+1][j] - subm[i][j]) inside {-4,4} ;
(subm[i][j+1] - subm[i][j]) inside {-4,4} ;
}
}
}
function print();
$display(“%p”,subm);
endfunction
endclass

module tb;
a2d_array a2d=new();
initial begin
a2d.randomize();
a2d.print();
end
endmodule