This code should work:
A = torch.tensor(
    [[1,2,5,8],
     [5,8,4,6],
     [9,1,6,7],
     [1,4,3,1]])
M = torch.ones(4, 4)
left_mask = torch.tensor([2, 0, 1, 3])
for idx in range(M.size(0)):
    M[idx, (A[idx, :left_mask[idx]+1]!=1.).nonzero()] = 0.