Easier and faster without a loop:
Z = [56,0,0,0,0,55; 0,0,0,0,0,0; 27,0,0,0,0,0; 0,0,0,0,0,0; 0,0,0,0,0,0; 100,0,0,0,0,25];
Z = Z(:, any(Z, 1)); % Keep only columns with any non-zero value Z = Z(any(Z, 2), :); % Keep only rows with any non-zero value
If you really want Z = [56,27,100; 55,0,25] as output, append:
Z = Z.'