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 Haskell (GHC 8.8.3) (judge verdict: Time Limit Exceeded)

{-# OPTIONS_GHC -Wno-tabs #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE BangPatterns #-}

-- Default Imports

import Control.Monad
import Control.Monad.ST
import Control.Monad.Trans.Class

import qualified Control.Monad.Reader as Reader
import Control.Monad.Reader (Reader, ReaderT, runReader, runReaderT)

import qualified Data.Array.MArray as MArray
import qualified Data.Array.ST as STArray
import Data.Array.ST (STArray, STUArray, runSTArray, runSTUArray)
import Data.Ix (Ix)

import qualified Data.Bits as Bits
import Data.Bits ((.&.), (.|.))

import qualified Data.ByteString.Char8 as BS
import Data.ByteString.Char8 (ByteString)

import qualified Data.Char as Char
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import qualified Data.List as List
import qualified Data.Map.Strict as Map
import Data.Map.Strict (Map)
import qualified Data.Maybe as Maybe
import qualified Data.Set as Set
import Data.Set (Set)

import qualified Data.Vector as Vector
import qualified Data.Vector.Generic as GVector
import qualified Data.Vector.Mutable as MVector
import Data.Vector.Mutable (STVector)
import qualified Data.Vector.Unboxed as UVector

import Numeric

import Debug.Trace
-- ----------

-- Custom Import Here


-- ----------

-- Default Definitions

type Vector = Vector.Vector
-- type GVector = GVector.Vector
type UVector = UVector.Vector

readIntToList :: IO [Int]
readIntToList = List.unfoldr (fmap (second $ BS.dropWhile Char.isSpace) . BS.readInt) <$> BS.getLine

readToList :: Read a => IO [a]
readToList = List.unfoldr (fmap (mapTaken . mapRemain) . span' (not . Char.isSpace)) <$> BS.getLine
	where
		mapTaken  = first $ read . BS.unpack
		mapRemain = second $ BS.dropWhile Char.isSpace
		span' pred s
			| BS.null taken = Nothing
			| otherwise     = Just (taken, remain')
			where
				(taken, remain) = BS.span pred s
				remain' = BS.dropWhile Char.isSpace remain

readToListN :: Read a => Int -> IO [a]
readToListN n = take n <$> readToList

readToTuple :: Read a => IO (a, a)
readToTuple = do
	[a, b] <- readToList
	return (a, b)

readIntToVector :: IO (Vector Int)
readIntToVector = Vector.unfoldr (fmap stripRemain . BS.readInt) <$> BS.getLine
	where stripRemain = second $ BS.dropWhile Char.isSpace

readIntToUVector :: IO (UVector.Vector Int)
readIntToUVector = UVector.unfoldr (fmap stripRemain . BS.readInt) <$> BS.getLine
	where stripRemain = second $ BS.dropWhile Char.isSpace

readIntToVectorN :: Int -> IO (Vector.Vector Int)
readIntToVectorN n = Vector.unfoldrN n (fmap stripRemain . BS.readInt) <$> BS.getLine
	where stripRemain = second $ BS.dropWhile Char.isSpace

readIntToUVectorN :: Int -> IO (UVector.Vector Int)
readIntToUVectorN n = UVector.unfoldrN n (fmap stripRemain . BS.readInt) <$> BS.getLine
	where stripRemain = second $ BS.dropWhile Char.isSpace


first :: (a -> a') -> (a, b) -> (a', b)
first f (a, b) = (f a, b)

second :: (b -> b') -> (a, b) -> (a, b')
second f (a, b) = (a, f b)

average :: Fractional a => [a] -> a
average xs = sum xs / (fromIntegral . length) xs

-- ----------

data STGrid s = STGrid {
	stXs :: STVector s Int,
	stYs :: STVector s Int,
	stBombs :: Set (Int, Int)
}

data Grid = Grid {
	xs :: Vector Int,
	ys :: Vector Int,
	bombs :: Set (Int, Int)
} deriving Show

freezeGrid :: STGrid s -> ST s Grid
freezeGrid (STGrid xs ys bombs) = do
	xs' <- Vector.unsafeFreeze xs
	ys' <- Vector.unsafeFreeze ys
	return $ Grid xs' ys' bombs

addToGrid :: STGrid s -> (Int, Int) -> ST s (STGrid s)
addToGrid (STGrid xs ys bombs) (y, x) = do
	MVector.modify xs (+ 1) (x - 1)
	MVector.modify ys (+ 1) (y - 1)
	let bombs' = Set.insert (x - 1, y - 1) bombs
	return $ STGrid xs ys bombs'


genGrid :: Int -> Int -> [(Int, Int)] -> Grid
genGrid h w bs = runST $ do
	init <- STGrid <$> MVector.replicate w 0 <*> MVector.replicate h 0 <*> pure Set.empty
	result <- foldM addToGrid init bs
	freezeGrid result

maxIndexes :: Vector Int -> (Int, [Int])
maxIndexes xs = foldr (maxIndexes' xs) (-1, []) [0..(Vector.length xs - 1)]

maxIndexes' :: Vector Int -> Int -> (Int, [Int]) -> (Int, [Int])
maxIndexes' xs i (m, cands)
	| i == Vector.length xs = (m, cands)
	| m' == m    = (m, i : cands)
	| m' > m     = (m', [i])
	| otherwise  = (m, cands)
	where m' = xs Vector.! i

main :: IO ()
main = do
	[h, w, m] <- readIntToList
	bs <- replicateM m readToTuple
	let grid = genGrid h w bs
	let (maxX, candsX) = maxIndexes (xs grid)
	let (maxY, candsY) = maxIndexes (ys grid)
	let cands = (,) <$> candsX <*> candsY
	print $ maxX + maxY - if all (flip Set.member $ bombs grid) cands
		then 1
		else 0