I don't think you need the for-loops here. Try this:
A = [1 2 0 0; 2 1 2 0; 0 2 1 2; 0 0 2 1];
B = [0 5 34 22; 5 0 34 21; 34 34 0 10; 22 21 10 0];
mask = (A~=0) & (B<=5);
If you need to know the rows and columns that meet the criteria, use find:
[rows,cols] = find(mask);