How to constraint a 2d matrix such that no two adjacent elements are the same Using sum() ? No two adjacent and diagonal elements should be the same

Any efficient way to code below?

module abc;
  class aa;
    rand bit [3:0] a1[7][7], val3;
     ///Working
    constraint c1{   
      foreach(a1[i])
      {
        foreach(a1[j]){
         if(i>0 &&i<7 &&j>0 && j<7)
         {
           a1[i][j] != a1[i+1][j];
           a1[i][j] != a1[i-1][j];
           a1[i][j] != a1[i][j+1];
           a1[i][j] != a1[i][j-1];
           a1[i][j] != a1[i+1][j+1];
           a1[i][j] != a1[i-1][j-1];
           a1[i][j] != a1[i-1][j+1];
           a1[i][j] != a1[i+1][j-1];
          }
         
           else if(i==0)
           {
             a1[i][j] != a1[i][j+1];
             a1[i][j] != a1[i+1][j];
           }
           else if(i==7)
           {
             a1[i][j] != a1[i][j+1];
             a1[i][j] != a1[i-1][j+1];
           }

           else if((j==7) || (j==0))
           {
            a1[i][j] != a1[i+1][j];
           }
        }
      }
         
  //Doesnt work

   constraint c2{   
      foreach(a1[i])
        foreach(a[j])
           a1[i].sum() with int'(item.index inside {[i+1:j-1]}) == 1;
             // foreach(a1[,j]) a1.sum() with (int'(a1[item.index][j] != 2));
        
        } 
       
   
  function void display();
    foreach (a1[i]) begin
      foreach (a1[i][j]) begin
        $write(" %d ",a1[i][j]);
      end
      $display("\n");
    end
  endfunction
                      
  endclass
 
  aa a2 = new();
 
  initial begin
    a2.randomize();
    a2.display();
    //$display("%p", a2.a1);
  end
endmodule

In reply to Quest4Knowledge:

I suppose there is an answer using the sum method, but you would have to offer a prize for the time spent on doing it that way.