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 Perl (5.26.1) (judge verdict: Time Limit Exceeded)

#!/usr/bin/perl
use Data::Dumper;

my ($h,$w,$m) = map { $_ - 0 } split(/\s+/,<STDIN>);
my @hw = ();
$#hw = $m - 1;

my @r_c = (); $#r_c = $h - 1;
my @c_c = (); $#c_c = $w - 1;
my @r_h = (); $#r_h = $h - 1;
my @c_h = (); $#c_h = $w - 1;

for(my $i=0;$i<$h;$i++){
  $r_c[$i] = +[$i,0];
}
for(my $i=0;$i<$w;$i++){
  $c_c[$i] = +[$i,0];
}

my @hw = ();
$#hw = $m - 1;
for(my $i=0;$i<$m;$i++){
  my @hw1 = map { $_ - 1 } split(/\s+/,<STDIN>);
  $hw[$i] = \@hw1;
}

for(my $i=0;$i<$m;$i++){
  my $h1 = $hw[$i]->[0];
  my $w1 = $hw[$i]->[1];
  $r_c[$h1]->[1] ++;
  $r_h[$h1]->{$w1} = 1;
  $c_c[$w1]->[1] ++;
  $c_h[$w1]->{$h1} = 1;
}

my @s_r_c = sort { $b->[1] <=> $a->[1] } @r_c ;
my @s_c_c = sort { $b->[1] <=> $a->[1] } @c_c ;

#print &Dumper(\@s_r_c);
#print &Dumper(\@s_c_c);

my $cnt = 0;
if( $s_r_c[0]->[1] > $s_c_c[0]->[1] ){
  $cnt += $s_r_c[0]->[1];
  my $row = $s_r_c[0]->[0];
  my @s_c_c2 = sort { ($b->[1] - $c_h[$b->[0]]->{$row}) <=> ($a->[1] - $c_h[$a->[0]]->{$row}) } @c_c ;
  $cnt += $s_c_c2[0]->[1] - $c_h[$s_c_c2[0]->[0]]->{$row};
} else {
  $cnt += $s_c_c[0]->[1];
  my $col = $s_c_c[0]->[0];
  my @s_r_c2 = sort { ($b->[1] - $r_h[$b->[0]]->{$col}) <=> ($a->[1] - $r_h[$a->[0]]->{$col}) } @r_c ;
  #print &Dumper(\@s_r_c2);
  $cnt += $s_r_c2[0]->[1] - $r_h[$s_r_c2[0]->[0]]->{$col};
}
print "$cnt\n";