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 Go (1.14.1) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func Scan() string {
	sc.Scan()
	return sc.Text()
}
func iScan() int {
	n, _ := strconv.Atoi(Scan())
	return n
}
func fScan() float64 {
	n, _ := strconv.ParseFloat(Scan(), 64)
	return n
}
func stringToInt(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}
func SScan(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		a[i] = Scan()
	}
	return a
}
func iSScan(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = iScan()
	}
	return a
}
func abs(x int) int {
	if x < 0 {
		x = -x
	}
	return x
}
func larger(a, b int) int {
	if a < b {
		return b
	} else {
		return a
	}
}
func smaller(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}
func max(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x < a[i] {
			x = a[i]
		}
	}
	return x
}
func min(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x > a[i] {
			x = a[i]
		}
	}
	return x
}
func sum(a []int) int {
	x := 0
	for _, v := range a {
		x += v
	}
	return x
}

var mod int = 1000000007

type con struct {
	a int
	b int
}

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, mod)
	sc.Split(bufio.ScanWords)
	//予備
	h := iScan()
	w := iScan()
	m := iScan()
	a := make([]int, m)
	b := make([]int, m)

	d1 := map[int]int{}
	d2 := map[int]int{}
	confirm := map[con]int{}
	for i := 0; i < m; i++ {
		a[i] = iScan()
		b[i] = iScan()
		var tmp con
		tmp.a = a[i]
		tmp.b = b[i]
		_, ok := confirm[tmp]
		v1, ok1 := d1[a[i]]
		v2, ok2 := d2[b[i]]
		if ok {
			continue
		} else {
			confirm[tmp] = 1
		}
		if ok1 {
			d1[a[i]] = v1 + 1
		} else {
			d1[a[i]] = 1
		}
		if ok2 {
			d2[b[i]] = v2 + 1
		} else {
			d2[b[i]] = 1
		}
	}
	maxA := 0
	maxB := 0

	var aitr int
	var bitr int
	aitr = 0
	bitr = 0

	ai := make([]int, 1)
	bi := make([]int, 1)
	for i := 1; i <= h; i++ {
		v, ok := d1[i]
		if ok {
			if maxA < v {
				maxA = v
				aitr = i
				ai = ai[0:1]
				ai = append(ai, aitr)
			} else if maxA == v {
				aitr = i
				ai = append(ai, aitr)
			}
		} else {
			continue
		}
	}
	for i := 1; i <= w; i++ {
		v, ok := d2[i]
		if ok {
			if maxB < v {
				maxB = v
				bitr = i
				bi = bi[0:1]
				bi = append(bi, bitr)
			} else if maxB == v {
				bitr = i
				bi = append(bi, bitr)
			}
		} else {
			continue
		}
	}
	ans := 0
	for i := 1; i < len(ai); i++ {
		for j := 1; j < len(bi); j++ {
			var tmp con
			tmp.a = ai[i]
			tmp.b = bi[j]
			_, ok := confirm[tmp]
			if ok {
				if ans < maxA+maxB-1 {
					ans = maxA + maxB - 1
				}
			} else {
				ans = maxA + maxB
				fmt.Println(ans)
				return
			}
		}
	}
	//fmt.Println(d1)
	//fmt.Println(d2)
	//fmt.Println(h, w)
	//fmt.Println(aitr, bitr)
	//fmt.Println(ai, bi)
	//fmt.Println(confirm)
	fmt.Println(ans)
}