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 C++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
 
typedef long long int ll;
typedef unsigned long long int ull;
 
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
typedef vector < pii > vpii;
typedef vector < pll > vpll;
 
typedef vector <string> vs;
typedef vector < vi > vvi;
typedef vector < vll > vvll;
 
#define fl(i, a, b)    for (int i(a); i <= (b); i ++)
#define rep(i, n)        fl (i, 1, (n))
#define loop(i, n)        fl (i, 0, (n) - 1)
#define rfl(i, a, b)    for (int i(a); i >= (b); i --)
#define rrep(i, n)        rfl (i, (n), 1)
 
#define all(v)     (v).begin(), (v).end()
#define srt(v)    sort (all (v))
 
#define pb push_back
#define mp make_pair
 
#define sz(x) ((int) (x).size())
#define fill (x, y)    memset(x, y, sizeof(x))
#define clr(a)    fill(a, 0)
 
#define PI 3.14159265358979323
 
const int Maxn = 1e5 + 1, Mod = 1e9 + 7;
#define trace1(x1)                    cerr << #x1 << ": " << x1 << endl;
#define trace2(x1, x2)                cerr << #x1 << ": " << x1 << " | " << #x2 << ": " << x2 << endl;
#define trace3(x1, x2, x3)            cerr << #x1 << ": " << x1 << " | " << #x2 << ": " << x2 << " | " << #x3 << ": " << x3 << endl;
 
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
 
const ll MOD = 1000000007LL;
const ll MAX = 100010LL;

// #define endl '\n'
 
// template <typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b);}
// template <typename T> T power(T x, T y, ll m = MOD) { T ans = 1; x %= m; while (y > 0) { if (y & 1ll) ans = (ans * x) % m; y >>= 1ll; x = (x * x)%m; } return ans%m;}
 
// template<typename T, typename T1>
// T mod(T x, T1 p) {
//     x %= p;
//     if (x < 0)
//         x += p;
//     return x;
// }
 
// int inv[Maxn], ifact[Maxn];
// template<typename T>
// T inverse(T x, T p) 
// {
//     x = mod(x, p);
//     if (x == 1)
//         return x;
//     return mod((1LL * (-p / x) * (inv[p % x] % p)) , p); 
//     // Since inverse of p % x is already calculated.
// }
 
// ifact[0] = 1;
// for(int i = 1; i < Maxn; i++) 
// {
//     inv[i] = inverse(i, Mod);
//     ifact[i] = (1LL * ifact[i - 1] * inv[i]) % Mod;
// }
 
// int fact[Maxn];
// fact[0] = 1;
// for(int i = 1; i < Maxn; i++) 
// {
//     fact[i] = 1LL * fact[i - 1] * i % Mod;
// }
 
// int n, t[4*MAXN];
// void build(int a[], int v, int tl, int tr) {
//     if (tl == tr) {
//         t[v] = a[tl];
//     } else {
//         int tm = (tl + tr) / 2;
//         build(a, v*2, tl, tm);
//         build(a, v*2+1, tm+1, tr);
//         t[v] = t[v*2] + t[v*2+1];
//     }
// }
// int sum(int v, int tl, int tr, int l, int r) {
//     if (l > r) 
//         return 0;
//     if (l == tl && r == tr) {
//         return t[v];
//     }
//     int tm = (tl + tr) / 2;
//     return sum(v*2, tl, tm, l, min(r, tm))
//            + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
// }
// void update(int v, int tl, int tr, int pos, int new_val) {
//     if (tl == tr) {
//         t[v] = new_val;
//     } else {
//         int tm = (tl + tr) / 2;
//         if (pos <= tm)
//             update(v*2, tl, tm, pos, new_val);
//         else
//             update(v*2+1, tm+1, tr, pos, new_val);
//         t[v] = t[v*2] + t[v*2+1];
//     }
// }
 
// int dif(string a, string b)
// {
// 	int cnt = 0;
// 	for(int i = 0 ; i < a.length() ; i++)
// 	{
// 		if(a[i]!=b[i])
// 			cnt++;
// 		if(cnt>1)
// 			return 0;
// 	}
// 	return 1;
// }

// void rv1(vector<int> &vec, int n) {
// 	vec = vector<int> (n);
// 	fl(i, 0, n-1) cin >> vec[i];
// 	return;
// }

// void pv1(vector<int> &vec) {
// 	int n = vec.size();
// 	if (n == 0) return;
// 	loop (i, n) cout << vec[i] << " ";
// 	return;
// }

// void rv2(vector<vector<int>> &vec, int n, int m) {
// 	vec = vector<vector<int>> (n, vector<int> (m));
// 	fl(i, 0, n-1) {
// 		fl(j, 0, m-1) cin >> vec[i][j];
// 	}
// 	return;
// }

// void pv2(vector<vector<int>> &vec) {
// 	int n = vec.size();
// 	if (n == 0) return;
// 	int m = vec[0].size();
// 	if (m == 0) return;
// 	loop (i, n) {
// 		loop (j, m) cout << vec[i][j] << " ";
// 		cout << endl;
// 	}
// 	return;
// }

int main() {
	FAST_IO;

	int T = 1;

	for (int t = 1; t <= T; t++) {
		int n, m, k;
		cin >> n >> m >> k;
		vpii row(n+1, mp(0, 0));
		vpii col(m+1, mp(0, 0));
		fl (i, 1, n) row[i].second = i;
		fl (i, 1, m) col[i].second = i;
		int x, y;
		set<pii> bs;
		fl (i, 1, k) {
			cin >> x >> y;
			bs.insert(mp(x, y));
			row[x].first++;
			col[y].first++;
		}
		sort(row.begin(), row.end());
		sort(col.begin(), col.end());
		int ans;
		int pr = row[n].first, pc = col[m].first;
		ans = pr + pc - 1;
		int i = n, j = m;
		bool f = false;
		while (i >= 1) {
			if (row[i].first != pr) break;
			j = m;
			while (j >= 1) {
				if (col[i].first != pc) break;
				if (bs.find(mp(row[i].second, col[i].second)) == bs.end()) {
					ans++;
					f = true;
					break;
				}
				j--;
			}
			if (f) break;
			i--;
		}
		cout << ans << endl;
	}

	return 0;
}