The law is called the Passenger Vessel Services Act (PVSA). It states that a foreign-flagged ship may not transport passengers from one US port to a different US port without making a call at a distant foreign port.
A distant foreign port is described as any port NOT "in North America, Central America, the Bermuda Islands, or the West Indies (including the Bahama Islands, but not including the Leeward Islands of the Netherlands Antilles, i.e., Aruba, Bonaire, and Curacao)." 19 CFR § 4.80a(a)(2)."
The exception is a cruise that begins or ends in Puerto Rico.
OK, with that background - While the first cruise is allowed, and the second cruise is allowed, together they are not allowed. The PVSA views B2B cruises as one cruise (as it's on the same ship). The B2B would transport you from NY to Miami without a stop in a distant foreign port.
If you're interested in reading all the legal-speak:
https://www.cbp.gov/sites/default/files/documents/pvsa_icp_3.pdf
A Panama Canal cruise typically calls at a
distant foreign port (usually Cartegena or Aruba).