import java.util.ArrayList;
import java.util.List;
public class OddEvenTree {
public int[] getTree
(String[] dist
) {
int[] noSolution = new int[1];
noSolution[0] = -1;
int n = dist.length;
int[] d = new int[n];
List<Integer> part0 = new ArrayList<Integer>(), part1 = new ArrayList<Integer>();
for(int i = 0; i < n; i++)
if(dist[0].charAt(i) == 'E') {
d[i] = 0;
part0.add(i);
}
else {
d[i] = 1;
part1.add(i);
}
if(d[0] == 1)
return noSolution;
if(part0.size() == 0 || part1.size() == 0)
return noSolution;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
int shouldBe = (d[i] + d[j]) % 2;
if(shouldBe == 0 && dist[i].charAt(j) == 'O')
return noSolution;
if(shouldBe == 1 && dist[i].charAt(j) == 'E')
return noSolution;
}
int[] ret = new int[(n-1)*2];
int p = 0;
for(int i = 0; i < part1.size(); i++)
{
ret[p] = part0.get(0);
p ++;
ret[p] = part1.get(i);
p ++;
}
for(int i = 1; i < part0.size(); i++)
{
ret[p] = part0.get(i);
p ++;
ret[p] = part1.get(0);
p ++;
}
return ret;
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKCgpwdWJsaWMgY2xhc3MgT2RkRXZlblRyZWUgewoKICAgIHB1YmxpYyBpbnRbXSBnZXRUcmVlKFN0cmluZ1tdIGRpc3QpCiAgICB7CiAgICAgICAgaW50W10gbm9Tb2x1dGlvbiA9IG5ldyBpbnRbMV07CiAgICAgICAgbm9Tb2x1dGlvblswXSA9IC0xOwoKICAgICAgICBpbnQgbiA9IGRpc3QubGVuZ3RoOwogICAgICAgIGludFtdIGQgPSBuZXcgaW50W25dOwogICAgICAgIExpc3Q8SW50ZWdlcj4gcGFydDAgPSBuZXcgQXJyYXlMaXN0PEludGVnZXI+KCksIHBhcnQxID0gbmV3IEFycmF5TGlzdDxJbnRlZ2VyPigpOwogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgICAgIGlmKGRpc3RbMF0uY2hhckF0KGkpID09ICdFJykgewogICAgICAgICAgICAgICAgZFtpXSA9IDA7CiAgICAgICAgICAgICAgICBwYXJ0MC5hZGQoaSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICBkW2ldID0gMTsKICAgICAgICAgICAgICAgIHBhcnQxLmFkZChpKTsKICAgICAgICAgICAgfQogICAgICAgIGlmKGRbMF0gPT0gMSkKICAgICAgICAgICAgcmV0dXJuIG5vU29sdXRpb247CiAgICAgICAgaWYocGFydDAuc2l6ZSgpID09IDAgfHwgcGFydDEuc2l6ZSgpID09IDApCiAgICAgICAgICAgIHJldHVybiBub1NvbHV0aW9uOwogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBuOyBqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGludCBzaG91bGRCZSA9IChkW2ldICsgZFtqXSkgJSAyOwogICAgICAgICAgICAgICAgaWYoc2hvdWxkQmUgPT0gMCAmJiBkaXN0W2ldLmNoYXJBdChqKSA9PSAnTycpCiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIG5vU29sdXRpb247CiAgICAgICAgICAgICAgICBpZihzaG91bGRCZSA9PSAxICYmIGRpc3RbaV0uY2hhckF0KGopID09ICdFJykKICAgICAgICAgICAgICAgICAgICByZXR1cm4gbm9Tb2x1dGlvbjsKICAgICAgICAgICAgfQoKICAgICAgICBpbnRbXSByZXQgPSBuZXcgaW50WyhuLTEpKjJdOwogICAgICAgIGludCBwID0gMDsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgcGFydDEuc2l6ZSgpOyBpKyspCiAgICAgICAgewogICAgICAgICAgICByZXRbcF0gPSBwYXJ0MC5nZXQoMCk7CiAgICAgICAgICAgIHAgKys7CiAgICAgICAgICAgIHJldFtwXSA9IHBhcnQxLmdldChpKTsKICAgICAgICAgICAgcCArKzsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBpID0gMTsgaSA8IHBhcnQwLnNpemUoKTsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgcmV0W3BdID0gcGFydDAuZ2V0KGkpOwogICAgICAgICAgICBwICsrOwogICAgICAgICAgICByZXRbcF0gPSBwYXJ0MS5nZXQoMCk7CiAgICAgICAgICAgIHAgKys7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICB9Cgp9Cg==