Basic solution: nested loops, one line at a time