Change the variable in your third loop from i to, say, k. The variable i is supposed to be a subset of m.