Line 65: sum += a[j][j];
should be sum += a[j][i];
(i.e. indices are j, then i, not two lots of j)
You have made life staggeringly difficult for yourself (and anybody reading your code) by continually flipping the role of i and j indices. This is one place where using row and col (or r and c) might make for more clarity than using the common counting variables i and j.