Maybe something like this ?
l = torch.arange(X+1)
r = torch.arange(X+1)

l = l.view(X+1, 1).repeat(1, X+1).view((X+1) * (X+1))
r = r.view(1, X+1).repeat(X+1, 1).view((X+1) * (X+1))

m = torch.stack([l, r], 1)