Better way to write nested loop, if condition and breaking it
My implementation:

(Is C#.Net but you'll be able to translate easily).

    public class Point()
    {
        public int X {get; set;}
        public int Y {get; set;}

        public Point (int x, int y)
        {
            this.X=x;
            this.Y=y;
        }
    }

    public void calcLine(bool[][] grid)
    {
        int rows = 500;
        int columns = 500;

        Point p1 = new Point(0, 0);
        // Change this outer 'if' to 'while' if should continue searching for all lines to draw
        if (canFindNextPoint(grid, rows, columns, p1))
        {
            Point p2 = new Point(p1.X + 1, 0);
            if (canFindNextPoint(grid, rows, columns, p2))
            {
                drawline(p1.X, p1.Y, p2.Y, p2.Y);
            }
        }
    }

    private bool canFindNextPoint(bool[][] grid, int rows, int columns, Point p)
    {
        bool found = false;
        for (int x = p.X; x &lt; rows; x++)
            for (int y = p.Y; y &lt; columns; y++)
            if (grid[x][y])
            {
                found = true;
                p.X = x;
                p.Y = y;
                break;
            }
        return found;
    }