top and left border

rewrite algorithm