Java, 257 246 210 bytes
a->{Integer r=9,i=512,j,x,y,k,p,z;l:for(;i-->0;){for(p=j=-1;++j<3;){x=y=0;for(k=9;k-->0;x+=k/3==j?z:0,y+=k%3==j?z:0)z=(i&1<<k)>0?7:a[k];p=p<0?x:p;if(x!=p|y!=p)continue l;}r=Math.min(r,r.bitCount(i));}return r;}
Saved 11 bytes thanks to ceilingcat.
Saved 36 bytes thanks to Olivier Grégoire.
Takes a flat array as input. This method tries all 512 (29) possibilities by using the binary representations of the integers from 0
to 511
. On each iteration, it sets the values at all indexes where i
has a set bit to 7
. Then, it checks if the current array is valid, in which case the answer is updated.