← Back to Problems
Pure Strategy Nash Equilibria
Easy standard Games & Strategy
Time limit: 1 s per test  ·  Memory: 256 MB

Two players play a one-shot game. The row player has m actions, the column player has n actions. If the row player picks action i and the column player picks action j, the row player receives payoff a_ij and the column player receives payoff b_ij.

A profile (i, j) is a pure strategy Nash equilibrium if neither player can strictly increase their own payoff by unilaterally switching to a different action: no k satisfies a_kj > a_ij, and no l satisfies b_il > b_ij.

Find all pure strategy Nash equilibria of the game.

Input

The first line contains m and n. The next m lines contain n integers each — the matrix A = (a_ij). The following m lines contain n integers each — the matrix B = (b_ij).

Output

On the first line, output the number of pure strategy Nash equilibria. Then output one equilibrium per line as two integers i and j (1-indexed), sorted lexicographically. If there are none, output 0.

Constraints

1 <= m, n <= 50

|a_ij|, |b_ij| <= 10^6

Scoring

This is a standard problem: a submission scores full points if it produces the correct output for every test case, and zero otherwise.

Examples

input
2 2
3 0
5 1
3 5
0 1
output
1
2 2
input
2 2
1 -1
-1 1
-1 1
1 -1
output
0
input
3 3
4 0 0
0 2 0
0 0 1
4 0 0
0 2 0
0 0 1
output
3
1 1
2 2
3 3
Python 3.13 i Execution environment Isolated microVM · 1 vCPU · no internet access Time and memory limits are set per problem Available packages numpy 2.5.0scipy 1.18.0pandas 3.0.0scikit-learn 1.9.0statsmodels 0.15.0cvxpy 1.9.2