# Java, <s>257</s> <s>246</s> <s>210</s> <s>200</s> <s>195</s> 190 bytes ``` a->{int r=9,i=512,j,e,s,k,x,y,z,b,c=0;for(;i-->0;r=e!=0|r<c?r:c)for(j=s=e=0;j<3;s=j<1?x:s,e|=x-s|y-s,j++)for(k=x=y=c=0;k<9;x+=k/3==j?z:0,y+=k++%3==j?z:0){c+=b=i>>k&1;z=b>0?7:a[k];}return r;} ``` *Saved 11 bytes thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904).* *Saved 46 bytes thanks to [Olivier Grégoire](https://codegolf.stackexchange.com/users/16236).* *Saved 10 bytes thanks to [Donat](https://codegolf.stackexchange.com/users/95987).* Takes a flat array as input. This method tries all 512 (2<sup>9</sup>) 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. [Try it online!](https://tio.run/##TZLBbqMwEIbvfQr30FUQA4UA3QQYol4q9bCn7i3KgVBSGShEtokgCc@eHXuTqBoZZn5/Mx6PXOWH3Kk@60vR5FKyP6cHtu@3DS@YVLmi36Hjn@w75y2bfSjB26/15tUiilWU6faKN@6ubwvFu9b927236u0apeSXX6VYbzK2Y3jJnezEW8UELoFj5M@hghIk1DDACEfYQoFesuvELOGOk3mJwPIRvbNIi5WIC0vvVCixJKpKg0RilfqrIZZQnnFw5Hl0JFS2bcAaBxxRF6zTZTLYWD8HiNXqGHswUmTbT7fYOhU2bpFnWf3LT464zbzV7zhf15tkEqXqRctEMl0SuvL9RuuNKqWSqOfATksIISALQXsLCCcwegS@0X3Q3hz8qx4Q90JcREY0eHc@MOQSIpMXXPUF@R6Rt3Wrr@vOaWn@Refc62vKN7TuYHHX9c7/E7RFV31OPUTmzNDY4kcdyjZd6W84kTzpQegRH3LB9BRiMwqLfYxSld9u1yt3T@9ENe1s5@b7fTO@ShrcTGOWRdnTw3T5Bw)