Scanf.scanf "%d %d %d" (fun w h n ->
    let rec loop i x0 y0 x1 y1 =
        if i = n then (x1 - x0) * (y1 - y0) else
            let x0, y0, x1, y1 = Scanf.scanf " %d %d %d" (fun x y a ->
                match a with
                | 1 -> if x > x1 then x, y0, x, y1 else max x x0, y0, x1,       y1
                | 2 -> if x < x0 then x, y0, x, y1 else x0,       y0, min x x1, y1
                | 3 -> if y > y1 then x0, y, x1, y else x0, max y y0, x1,       y1
                | 4 -> if y < y0 then x0, y, x1, y else x0,       y0, x1, min y y1
                | _ -> failwith "??"
            )
            in
            loop (i + 1) x0 y0 x1 y1
    in
    loop 0 0 0 w h |> Printf.printf "%d\n"
)