CCCPaste
--https://pastebin.com/

function forward(n)
    for _ = 1, n do
        turtle.forward()
    end
end

function back(n)
    for _ = 1, n do
        turtle.back()
    end
end

function mine3(n)
    for _ = 1, n do
        turtle.dig()
        while not turtle.forward() do
            digLoose()
        end
        turtle.digUp()
        safeDigDown()
        digLooseUp()
    end
end

-- in foo turtle must make exactly 1 step forward, no more steps, no rotates. foo args is x and y coords of cell after forward step
function handleSquare(a, b, foo)
    if a < 2 or b < 2 then
        print("dimensions must be positive!")
        return
    end
    local needFuel = a * b + a + b - 1 - a * (1 - b % 2)
    if turtle.getFuelLevel() < needFuel then
        print("Not enough fuel! Must be at least " .. needFuel)
        print("Try to add " .. math.ceil((needFuel - turtle.getFuelLevel()) / 80) .. " coal")
        return
    end
    for y = 0, a - 1 do
        foo(0, y)
    end
    for x = 1, b - 2, 2 do
        turtle.turnRight()
        foo(x, a - 1)
        turtle.turnRight()
        for y = a - 2, 0 do
            foo(x, y)
        end
        turtle.turnLeft()
        foo(x + 1, 0)
        turtle.turnLeft()
        for y = 1, a - 1 do
            foo(x + 1, y)
        end
    end
    if b % 2 == 0 then
        turtle.turnRight()
        foo(b - 1, a - 1)
        turtle.turnRight()
        for y = a - 2, 0 do
            foo(b - 1, y)
        end
        turtle.turnRight()
        forward(b - 1)
        turtle.turnRight()
        turtle.back()
    else
        turtle.turnLeft()
        forward(b - 1)
        turtle.turnRight()
        back(a)
    end
end

function test(x, y)
    turtle.forward()
    print(x, y)
end

handleSquare(2, 2, test)