function [output] = parero(X,scaling)

sx = scaling(1);
sy = scaling(2);

if (sx < 0) || (sy < 0)
    disp('The scaling factor must be >= 0.');
    return;
end

[m,n] = size(X);

Z = X;

Xshift = X;
for shift = 1:ceil(sqrt(1/sx))
    Xshift(:,2:n) = Xshift(:,1:n-1);
    Xshift(:,1) = Inf;
    Z = min(Z, Xshift + sx * shift * shift);
end

Xshift = X;
for shift = 1:ceil(sqrt(1/sx))
    Xshift(:,1:n-1) = Xshift(:,2:n);
    Xshift(:,n) = Inf;
    Z = min(Z, Xshift + sx * shift * shift);
end

X = Z;

Xshift = X;
for shift = 1:ceil(sqrt(1/sy))
    Xshift(2:m,:) = Xshift(1:m-1,:);
    Xshift(1,:) = Inf;
    Z = min(Z, Xshift + sy * shift * shift);
end

Xshift = X;
for shift = 1:ceil(sqrt(1/sy))
    Xshift(1:m-1,:) = Xshift(2:m,:);
    Xshift(m,:) = Inf;
    Z = min(Z, Xshift + sy * shift * shift);
end

output = Z;

end