The condition "i==1 && j==m" appears twice:
if i==1 && j==m
elseif (i==1) && (j>1 || j<m)
elseif (i==1) && (j==m)
If the first condition is met already, further elseif branches are not called anymore. The if block is left directly after the first condition is matching and the corresponding block was executed.