(* Set the maximum values for l and m *)lMax = 5; (* For example, up to l = 5 *)mMax = 5; (* For example, up to m = 5 *)(* Create a list of headers for l and m *)lHeaders = Prepend[Table[l, {l, 0, lMax}], "l\m"];mHeaders = Table[m, {m, 0, mMax}];(* Create the 2D table with associated Legendre polynomials, including the m header *)assocLegendreTable = Simplify[Table[LegendreP[l, m, x], {l, 0, lMax}, {m, 0, Min[l, mMax]}]];assocLegendreTableWithHeaders = Prepend[assocLegendreTable, mHeaders];(* Insert the l headers *)assocLegendreTableWithHeaders = MapThread[Prepend, {assocLegendreTableWithHeaders, lHeaders}];(* Use Grid to display the table with headers and grid lines *)Grid[assocLegendreTableWithHeaders, Frame -> All]