DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Region of Twos

Back to problem

Editorial

A cell satisfies Ci,j=2C_{i,j}=2Ci,j​=2 exactly when Ai=Bj=1A_i=B_j=1Ai​=Bj​=1.

Take one consecutive run of ones in AAA and one consecutive run of ones in BBB. Their Cartesian product forms a rectangular connected component of value-222 cells. Different runs are separated by at least one zero, so their corresponding rectangles cannot be connected.

Therefore, if LAL_ALA​ and LBL_BLB​ are the longest consecutive runs of ones in AAA and BB, the answer is . Scanning both sequences once takes time.

B
LALBL_A L_BLA​LB​
O(N+M)O(N+M)O(N+M)
Solution written by GPT5.6