Change
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
to
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
and I haven't analyzed your code properly, but a guess is that
return lookUpChain(m, p, 1, n);
should be
return lookUpChain(m, p, 1, n - 1);