System-verilog constraint for 2D array, where value of an element should not match to any of its neighbors

In reply to gst_beginner :

constraint not_neighbor { foreach (arr[i][j]) {
        i>0 && j>0             -> arr[i][j] != { arr[i-1][j-1];
        i>0 &&                 -> arr[i][j] != { arr[i-1][j];
        i>0 && j< $size(arr,2)-1 -> arr[i][j] != { arr[i-1][j+1];
        ... // I'll let you fill this in
        i<$size(arr,1)-1 && j< $size(arr,2)-1 -> arr[i][j] != { arr[i+1][j+1];
} }

DO check this for proper operator precedence.