function costs = seam( M )
[m, n] = size(M);
costs = M;
costs(1, :) = M(1, :);
for i = 2:m
    for j = 1:n
        minimum = min(costs(i-1, max(j-1, 1):min(j+1, n)));
        costs(i, j) = costs(i, j) + minimum;
    end
end
end

