Problem:
Score : 500 points

Problem Statement

We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i-th target is \left(h_i, w_i \right).

Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.

Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

Constraints

All values in input are integers.

1 \leq H, W \leq 3 \times 10^5

1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)

1 \leq h_i \leq H

1 \leq w_i \leq W

\left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)

Input

Input is given from Standard Input in the following format:

H W M
h_1 w_1
\vdots
h_M w_M

Output

Print the answer.

Sample Input 1

2 3 3
2 2
1 1
1 3

Sample Output 1

3

We can destroy all the targets by placing the bomb at \left(1, 2\right).

Sample Input 2

3 3 4
3 3
3 1
1 1
1 2

Sample Output 2

3

Sample Input 3

5 5 10
2 5
4 3
2 3
5 5
2 2
5 4
5 3
5 1
3 5
1 4

Sample Output 3

6

Solution in Java (OpenJDK 11.0.6) (judge verdict: Time Limit Exceeded)

import java.util.*;

public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            // # In
            // area
            int H = Integer.parseInt(sc.next());    // height
            int W = Integer.parseInt(sc.next());    // width

            // mine
            int M = Integer.parseInt(sc.next());
            int[] hnum = new int[H];
            int[] wnum = new int[W];
            List<Integer>[] mines = new ArrayList[H];
            for (int i = 0; i < M; i++) {
                int hi = Integer.parseInt(sc.next()) - 1;
                int wi = Integer.parseInt(sc.next()) - 1;
                hnum[hi]++;
                wnum[wi]++;

                if (mines[hi] == null) {
                    mines[hi] = new ArrayList<Integer>();
                    mines[hi].add(wi);
                } else {
                    mines[hi].add(wi);
                }
            }

            // # Solve
            int hmax = 0, wmax = 0;
            List<Integer> hmax_index = new ArrayList<>();
            List<Integer> wmax_index = new ArrayList<>();
            for (int i = 0; i < H; i++) {
                if (hmax < hnum[i]) {
                    hmax = hnum[i];
                    hmax_index.clear();
                    hmax_index.add(i);
                } else if (hmax == hnum[i]) {
                    hmax_index.add(i);
                }
            }
            for (int i = 0; i < W; i++) {
                if (wmax < wnum[i]) {
                    wmax = wnum[i];
                    wmax_index.clear();
                    wmax_index.add(i);
                } else if (wmax == wnum[i]) {
                    wmax_index.add(i);
                }
            }

            boolean hit = false;
            for (int h : hmax_index) {
                for (int maxw : wmax_index) {
                    if (!mines[h].contains(maxw)) {
                        hit = true;
                    }
                }
                if (hit) break;
            }
            System.out.println(hmax + wmax + (hit ? 0 : -1));
        }
    }
}