The cause of your problem is the fact that the clone method of a 2D array creates a
shallow copy. (Stupid? Yes.) Think of a 2D array as an object with X
references to Y 1D arrays (each of which contain additional references--the actual objects stored in the array). The clone method of a 2D array creates a new 2D array--but copies the references to the 1D arrays rather than cloning them! You can avoid this by writing your own method to copy a 2D array:
public static int[][] copy(int[][] a) {
int[][] b = new int[a[0].length][a.length];
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
b[i][j] = a[i][j];
return b;
} Hopefully
that will solve your problem.
