(* This Mathematica program is used to calculate the partition function coefficients of the 2-dimensional Ising model. n is the height and m is the width of the lattice.

The partition function Zn,m(K) is given by

Zn,m(K)=e2nmK Sum[k=0,nm,gk x2k]
where x=e-2K. For n>5 and m>5 the first few terms in the series are
Zn,m(K)=e2nmK(2+(2mn)x4 + (4mn)x6+ ((mn)2+9mn)x8+ (4(mn)2+24mn)x10+...)

gk represents the number of different states that have energy 2kJ above the ground state. If n and m are even then the coefficients have a ferromagnetic-antiferromagnetic symmetry which gives gk=gmn-k.
g0=2 (All up and all down)
g1=0
g2=2mn (A single flipped spin)
g3=4mn (A pair of flipped spins)
g4=(mn)2+9mn
g5=4(mn)2+24mn

For example, the 32x32 lattice has the following partition function.

Z32,32(K) = e2048K ( 2 + 2048 x4 + 4096 x6 +
1057792 x8 + 4218880 x10 + 371621888 x12 +
2191790080 x14 + 100903637504 x16 +
768629792768 x18 + 22748079183872 x20 +
205394991329280 x22 + 4449893079195648 x24 +
44743561136807936 x26 +
776335369907888128 x28 +
8296338816339398656 x30 +
122868315209973770496 x32 +
1348964107338697842688 x34 +
17832940430689993684992 x36 +
196523971814021975830528 x38 +
2391406180740483290926080 x40 +
26065481800659863486496768 x42 +
298044124622553465145298944 x44 +
3185666524478313863851044864 x46 +
34699120806036636726464401408 x48 +
362130971398695260784899284992 x50 +
3791345208840452468065820620800 x52 +
38569361472886182609543396929536 x54 +
390468333064795797623009891880960 x56 +
3871553755823196043657602074615808 x58 +
38057017987760849668592216619089920 x60 +
368038492491826519223092138855485440 x62 +
3523173455605142701375696893142359104 x64 + ...
4096 x2042 + 2048 x2044 + 2 x2048 )

The largest coefficient for the 32x32 lattice is g512. Its value is

g512 = 6,342,873,169,001,916,568,766,443,273,025,000,331,593,063,924,
436,135,196,680,443,689,656,478,072,741,300,511,612,123,900,652,711,
596,311,283,701,724,071,226,144,241,851,411,641,714,893,727,789,741,
510,169,213,344,005,116,385,197,594,692,089,556,614,547,788,150,860,
200,720,413,211,442,412,355,672,291,841,364,265,145,274,980,444,405,
423,129,672,679,584,959,498,234,944,801,613,246,300,853,599,317,229,
362,316 = 6.342873169 x 10306 .

The program below calculates all of the coefficients exactly, including this 307 digit result.

n = 8; m = 8;
Clear[a, b, c, s, c2, s2, x, z, coef, z1, z2, z3, z4];
acc = Floor[N[n m Log[2]/Log[10]] 1.5];
If[Mod[n, 2] == 0, z1 = 2^(m n/2 - 1) Product[c2[k], {k, 1, n - 1, 2}];
z2 = 2^(m n/2 - 1) Product[s2[k], {k, 1, n - 1, 2}];
z3 = 2^(m n/2 - 1) c[0] c[n] Product[c2[k], {k, 2, n - 2, 2}];
z4 = 2^(m n/2 - 1) s[0] s[n] Product[s2[k], {k, 2, n - 2, 2}],
z1 = 2^(m n/2 - 1) c[n] Product[c2[k], {k, 1, n - 1, 2}];
z2 = 2^(m n/2 - 1) s[n] Product[s2[k], {k, 1, n - 1, 2}];
z3 = 2^(m n/2 - 1) c[0] Product[c2[k], {k, 2, n - 1, 2}];
z4 = 2^(m n/2 - 1) s[0] Product[s2[k], {k, 2, n - 1, 2}]];
z1 + z2 + z3 + z4;
c[0] = ((1 - x)^m + (x(1 + x))^m)/2^(m/2);
s[0] = ((1 - x)^m - (x(1 + x))^m)/2^(m/2);
c[n] = ((1 + x)^m + (x(1 - x))^m)/2^(m/2);
s[n] = ((1 + x)^m - (x(1 - x))^m)/2^(m/2);
b = 2x(1 - x^2);
a[k_] = (1 + x^2)^2 - b Cos[Pi k/n];
c2[k_] = 2^(1 - 2m)(Sum[ m!/j!/(m - j)!(a[k]^2 - b^2)^(j/2) a[k]^(m - j), {j, 0, m, 2}] + b^m);
s2[k_] = 2^(1 - 2m)(Sum[ m!/j!/(m - j)!(a[k]^2 - b^2)^(j/2) a[k]^(m - j), {j, 0, m, 2}] - b^m);
z = Rationalize[Chop[Expand[N[z1 + z2 + z3 + z4, acc]]]];

coef = Table[0, {m n + 1}];
Do[coef[[j + 1]] = Coefficient[z, x, 2j], {j, 0, m n}];

If[Apply[Plus, coef] != 2^(n m),
Print["Error in calculation: Sum of coefficients incorrect."],

Print[coef]]

The results for n x m = 8 x 8 are given below as a check.

{2, 0, 128, 256, 4672, 17920, 145408, 712960, 4274576, 22128384, 118551552, 610683392, 3150447680, 16043381504, 80748258688, 396915938304, 1887270677624, 8582140066816, 36967268348032, 149536933509376, 564033837424064, 1971511029384704, 6350698012553216, 18752030727310592, 50483110303426544, 123229776338119424, 271209458049836032, 535138987032308224, 941564975390477248, 1469940812209435392, 2027486077172296064, 2462494093546483712, 2627978003957146636, 2462494093546483712, 2027486077172296064, 1469940812209435392, 941564975390477248, 535138987032308224, 271209458049836032, 123229776338119424, 50483110303426544, 18752030727310592, 6350698012553216, 1971511029384704, 564033837424064, 149536933509376, 36967268348032, 8582140066816, 1887270677624, 396915938304, 80748258688, 16043381504, 3150447680, 610683392, 118551552, 22128384, 4274576, 712960, 145408, 17920, 4672, 256, 128, 0, 2}