Given a binary matrix **mat[][] **of dimensions** M x N **and two-person **P1, P2**, the task is to find the person who finishes last in choosing a **0 **from the matrix which changes to **1 **only if the row or column of the cell consisting of **0 **has one or more than one **1 **in the matrix.**Note:** P1 starts picking the **0s** first and both the persons want to finish last. The given matrix will always have at least one **0 **which could be chosen.

**Examples:**

Input: mat[][] = {{1, 0, 0}, {0, 0, 0}, {0, 0, 1}}Output: P1Explanation:

P1 chooses mat[1][1], then the matrix becomes {{1, 0, 0}, {0, 1, 0}, {0, 0,1}}.

P2 has no0left to choose from. So, P1 finishes last.

Input: mat[][] = {{0, 0}, {0, 0}}Output: P2Explanation:

No matter P1 chooses which0P2 will always have a0to choose and

after P2 picks a0there will not be any other0to choose from.

**Approach: **The idea is based on the observation that a **0** can’t be taken if either of its row or column has **1. **Follow the steps below to solve this problem:

- Initialize two sets,
**rows**&**cols**to count the number of rows and columns which does not contain any**1**. - Traverse the matrix and add rows and columns having
**1**in it in the set. - Take the minimum number of rows or columns as if either of them becomes zero so that no more
**0**s can be taken. - After finding the minimum number of rows and columns available, if the number of choices made is odd then
**P1**finishes last otherwise,**P2**finishes last.

Below is the implementation of the above approach:

## Python3

`# Python3 program for the above approach` ` ` `# Function to find the person` `# who will finish last` `def` `findLast(mat):` ` ` ` ` `m ` `=` `len` `(mat)` ` ` `n ` `=` `len` `(mat[` `0` `])` ` ` ` ` `# To keep track of rows` ` ` `# and columns having 1` ` ` `rows ` `=` `set` `()` ` ` `cols ` `=` `set` `()` ` ` ` ` `for` `i ` `in` `range` `(m):` ` ` `for` `j ` `in` `range` `(n):` ` ` `if` `mat[i][j]:` ` ` `rows.add(i)` ` ` `cols.add(j)` ` ` ` ` `# Available rows and columns` ` ` `avRows ` `=` `m` `-` `len` `(` `list` `(rows))` ` ` `avCols ` `=` `n` `-` `len` `(` `list` `(cols))` ` ` ` ` `# Minimum number of choices we have` ` ` `choices ` `=` `min` `(avRows, avCols)` ` ` ` ` `# If number of choices are odd` ` ` `if` `choices & ` `1` `:` ` ` ` ` `# P1 will finish last` ` ` `print` `(` `'P1'` `)` ` ` ` ` `# Otherwise, P2 will finish last` ` ` `else` `:` ` ` `print` `(` `'P2'` `)` ` ` ` ` `# Given matrix` `mat ` `=` `[[` `1` `, ` `0` `, ` `0` `], [` `0` `, ` `0` `, ` `0` `], [` `0` `, ` `0` `, ` `1` `]]` ` ` `findLast(mat)` |

**Output:**

P1

**Time Complexity:** O(M*N)**Auxiliary Space:** O(M*N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**