[h,w]=size(a) [h w]=size(a) b=0 for s=1:min(h,w) for i=1:h-s for j=1:w-s l=a(i:s+i,j:s+j) if min(l)==max(l) & sum(l(:))>b, b=sum(l(:)),end; end end end