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 (Python):



"""






















using namespace std;

using int64 = long long;




int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int h, w, m; cin >> h >> w >> m;
  vector<pair<int, int>> in;

  vector<int> cnt_row(h, 0), cnt_col(w, 0);

  for (int i = 0; i < m; ++i) {
    int hh, ww; cin >> hh >> ww;
    --hh; --ww;
    in.emplace_back(hh, ww);
    ++cnt_row[hh]; ++cnt_col[ww];
  }

  int mx_row = *max_element(all(cnt_row));
  int mx_col = *max_element(all(cnt_col));
  int ans = mx_row + mx_col;
  int64 can = count(all(cnt_row), mx_row) * count(all(cnt_col), mx_col);
  if (can > m) {
    cout << ans << endl;
  } else {
    int bad = 0;
    for (auto e : in) bad += (cnt_row[e.first] + cnt_col[e.second] == ans);
    if (can > bad) {
      cout << ans << endl;
    } else {
      cout << ans - 1 << endl;
    }
  }

  return 0;
}

"""

import base64
import subprocess
import zlib


exe_bin = "c%1E9eQ+Dcb>9O(5t1kX`mjt%mI*tQL&XwINR&jxkwuUMd5DiP!w*|+EuBCB6k&q^0~{n$Bd1JBy1CGB7>|>vjWSi;W~N``DCsnHKRh8

open("./kyomu", 'wb').write(zlib.decompress(base64.b85decode(exe_bin)))
subprocess.run(["chmod +x ./kyomu"], shell=True)
subprocess.run(["./kyomu"], shell=True)

Solution (Python):
H,W,M=map(int,input().split())
h=[0]*(H+1)
w=[0]*(W+1)
hw=[0]*M
for i in range(M):
  y,x=map(int,input().split())
  hw[i]=(y,x)
  h[y]+=1
  w[x]+=1
cnt=0
mxh=max(h)
mxw=max(w)
for hi,wi in hw:
  if h[hi]==mxh and w[wi]==mxw:
    cnt+=1
if h.count(mxh)*w.count(mxw)==cnt:
  print(mxh+mxw-1)
else:
  print(mxh+mxw)

Solution (Python):
H,W,M = map(int, input().split())

hs = [0] * H
ws = [0] * W
s = set()
for i in range(M):
  h,w = map(int, input().split())
  h -= 1
  w -= 1
  hs[h] += 1
  ws[w] += 1
  s.add((h,w))
  
mh = 0
mw = 0
for i in range(H):
  mh = max(mh, hs[i])
for j in range(W):
  mw = max(mw, ws[j])
  
i_s = list()
j_s = list()
for i in range(H):
  if hs[i] == mh:
    i_s.append(i)
for j in range(W):
  if ws[j] == mw:
    j_s.append(j)
    
ans = mh + mw
for i in i_s:
  for j in j_s:
    if (i,j) in s:
      continue
    print(ans)
    exit()
ans -= 1
print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline
 
H, W, M = map(int, input().split())
 
X = [0] * W
Y = [0] * H
Map = []
 
for _ in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    Y[h] += 1
    X[w] += 1
    Map.append((h, w))
 
MX = max(X)
MY = max(Y)
ans = MX + MY
Xans = set()
Yans = set()
for i, x in enumerate(X):
    if x == MX:
        Xans.add(i)
for i, y in enumerate(Y):
    if y == MY:
        Yans.add(i)
cnt = 0
for h, w in Map:
    if h in Yans and w in Xans:
        cnt += 1
if cnt == len(Xans) * len(Yans):
    ans -= 1
print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline

H, W, M = map(int, input().split())

X = [0] * W
Y = [0] * H
Map = []

for _ in range(M):
    h, w = map(int, input().split())
    h -= 1
    w -= 1
    Y[h] += 1
    X[w] += 1
    Map.append((h, w))

MX = max(X)
MY = max(Y)
ans = MX + MY
Xans = set()
Yans = set()
for i, x in enumerate(X):
    if x == MX:
        Xans.add(i)
for i, y in enumerate(Y):
    if y == MY:
        Yans.add(i)
cnt = 0
for h, w in Map:
    if h in Yans and w in Xans:
        cnt += 1
if cnt == len(Xans) * len(Yans):
    ans -= 1
print(ans)

Solution (Python):




import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    h,w,m=map(int, readline().rstrip().split())
    cntH=[0]*h
    cntW=[0]*w

    ys=[0]*m
    xs=[0]*m

    for i in range(m):
        y,x = map(int, readline().rstrip().split())
        y-=1
        x-=1
        cntH[y]+=1
        cntW[x]+=1
        ys[i]=y
        xs[i]=x
    maxX = max(cntW)
    maxY = max(cntH)
    cntMaxX=cntW.count(maxX)
    cntMaxY=cntH.count(maxY)
    cntMax=cntMaxX*cntMaxY

    for i in range(m):
        y=ys[i]
        x=xs[i]
        maxPtn=False
        if cntH[y]==maxY and cntW[x]==maxX:
            cntMax-=1
    if cntMax > 0:
        print(maxX+maxY)
    else:
        print(maxX+maxY-1)
    return


if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):
H,W,M = map(int, input().split())

hs = [0] * H
ws = [0] * W
s = set()

for i in range(M):
  h,w = map(int, input().split())
  h -= 1
  w -= 1
  hs[h] += 1
  ws[w] += 1
  s.add((h,w))

mh = 0
mw = 0
for i in range(H):
  mh = max(mh, hs[i])
for j in range(W):
  mw = max(mw, ws[j])
  
l_s = list()
j_s = list()
for i in range(H):
  if mh == hs[i]:
    l_s.append(i)
for j in range(W):
  if mw == ws[j]:
    j_s.append(j)
    
ans = mh+mw
for i in l_s:
  for j in j_s:
    if (i,j) in s:
      continue
    print(ans)
    exit()
ans -= 1
print(ans)

Solution (Python):
H, W, M = map(int, input().split())

sumh = [0] * W
sumw = [0] * H
checkset = set()
for i in range(M):
    h, w = map(int, input().split())
    checkset.add((h-1, w-1))
    sumh[w-1] += 1
    sumw[h-1] += 1
shmax = max(sumh)
swmax = max(sumw)
sh = []
sw = []
for i, s in enumerate(sumh):
    if s == shmax: sh.append(i)
for i, s in enumerate(sumw):
    if s == swmax: sw.append(i)
ans = shmax + swmax - 1
jdg = False
for i in sh:
    for j in sw:
        if (j, i) not in checkset: 
            ans += 1
            jdg = True
            break

    if jdg: break
print(ans)

Solution (Python):
from collections import Counter


def main():
    H, W, M = list(map(int, input().split()))
    bombs = [list(map(int, input().split())) for _ in range(M)]
    counter_row = Counter([h for h, _ in bombs])
    val_max_row, max_rows = 0, []
    for h, v in counter_row.items():
        if val_max_row < v:
            val_max_row = v
            max_rows = [h]
        elif val_max_row == v:
            max_rows.append(h)
    counter_col = Counter([w for _, w in bombs])
    val_max_col, max_cols = 0, []
    for w, v in counter_col.items():
        if val_max_col < v:
            val_max_col = v
            max_cols = [w]
        elif val_max_col == v:
            max_cols.append(w)
    
    
    max_rows = Counter(max_rows)
    max_cols = Counter(max_cols)
    n_max_cells = len(max_rows.keys()) * len(max_cols.keys())
    n_cells = 0
    for h, w in bombs:
        if max_rows[h] > 0 and max_cols[w] > 0:
            n_cells += 1
    ans = val_max_row + val_max_col
    if n_cells >= n_max_cells:
        ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

h,w,m = (int(x) for x in input().split())
rows = [[0,i] for i in xrange(h)]
cols = [[0,i] for i in xrange(w)]
bomb_dict = dict()
for i in xrange(m):
    hi,wi = (int(x)-1 for x in input().split())
    rows[hi][0] += 1
    cols[wi][0] += 1
    bomb_dict[(hi,wi)] = True

rows = sorted(rows, reverse=True)
cols = sorted(cols, reverse=True)
row_val = rows[0][0]
col_val = cols[0][0]
ans = None
for row in rows:
    if row[0] != row_val:
        break
    for col in cols:
        if col[0] != col_val:
            break
        getr = bomb_dict.get((row[1], col[1]), False)
        if not getr:
            ans = row_val + col_val
            break
    
    if ans:
        break

if ans is None:
    ans = row_val + col_val - 1
print(ans)

Solution (Python):
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

h,w,m = (int(x) for x in input().split())
rows = [[0,i] for i in xrange(h)]
cols = [[0,i] for i in xrange(w)]
bomb_dict = dict()
for i in xrange(m):
    hi,wi = (int(x)-1 for x in input().split())
    rows[hi][0] += 1
    cols[wi][0] += 1
    bomb_dict[(hi,wi)] = True

rows = sorted(rows, reverse=True)
cols = sorted(cols, reverse=True)

ans = 1
for row in rows:
    ans_ans = None
    for col in cols:
        getr = bomb_dict.get((row[1], col[1]), False)
        if getr and ans_ans is None:
            ans_ans = row[0] + col[0] - 1
            
            best_ans = ans_ans
        elif getr and row[0] + col[0]-1 < ans_ans:
            best_ans = ans_ans
            break
        elif getr:
            pass
        else:
            if ans_ans is None:
                best_ans = row[0] + col[0]
                
            else:
                best_ans = max(row[0] + col[0], ans_ans)
                
            break
    if best_ans > ans:
        ans = best_ans
    if best_ans < ans:
        break
    
print(ans)

Solution (Python):
import sys


H, W, M = map(int, input().split())
bomb = [tuple(map(lambda x: int(x) - 1, s.split())) for s in sys.stdin.readlines()]
X = [0] * H  
Y = [0] * W  
for h, w in bomb:
    X[h] += 1
    Y[w] += 1
maxX = max(X)
maxY = max(Y)

R = [h for h, x in enumerate(X) if x == maxX]  
C = [w for w, y in enumerate(Y) if y == maxY]  

bomb = set(bomb)
for r in R:
    for c in C:
        if (r, c) not in bomb:
            
            
            print(maxX + maxY)
            exit()
print(maxX + maxY - 1)

Solution (Python):
import sys
input = sys.stdin.readline
H, W, M = map(int, input().split())


vert = [0] * (2*W)

def update(i, d):
	i += W
	vert[i] += d
	while i > 1:
		i >>= 1
		vert[i] = max(vert[i*2], vert[i*2+1])


lines = [[] for _ in range(H)]
for _ in range(M):
	y, x = map(lambda s: int(s)-1, input().split())
	lines[y].append(x)
	update(x, 1)

best = 0

for l in lines:
	for x in l: update(x, -1)
	best = max(best, len(l) + vert[1])
	for x in l: update(x, 1)

print(best)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import Counter, defaultdict


def read():
    H, W, M = map(int, input().strip().split())
    HW = []
    for i in range(M):
        h, w = map(int, input().strip().split())
        HW.append((h, w))
    return H, W, M, HW


def solve(H, W, M, HW):
    hcount = Counter()
    wcount = Counter()
    for h, w in HW:
        hcount[h] += 1
        wcount[w] += 1
    hm = hcount.most_common()
    wm = wcount.most_common()
    hmax = hm[0][1]
    wmax = wm[0][1]
    hn = len([1 for k, v in hcount.items() if v == hmax])
    wn = len([1 for k, v in wcount.items() if v == wmax])
    m = hn * wn
    for h, w in HW:
        if hcount[h] == hmax and wcount[w] == wmax:
            m -= 1
    if m == 0:
        return hmax + wmax - 1
    return hmax + wmax


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Solution (Python):
h,w,m=map(int,input().split())

st=set()
h_b=[0 for i in range(h)]
w_b=[0 for i in range(w)]
for i in range(m):
  th,tw=map(int,input().split())
  th-=1
  tw-=1
  st.add((th,tw))
  h_b[th]+=1
  w_b[tw]+=1

h_max=max(h_b)
w_max=max(w_b)

h_cand=[i for i in range(h) if h_b[i]==h_max]
w_cand=[i for i in range(w) if w_b[i]==w_max]

for i in h_cand:
  for j in w_cand:
    if (i,j) not in st:
      print(h_max+w_max)
      exit()

print(h_max+w_max-1)

Solution (Python):
H, W, M = map(int, input().split())
h_list = [0] * H
w_list = [0] * W
target = []
for _ in range(M):
  h, w = map(int, input().split())
  h_list[h-1] += 1
  w_list[w-1] += 1
  target += [[h-1,w-1]]

h_max = max(h_list)
w_max = max(w_list)
h_max_list = set([i for i, v in enumerate(h_list) if v == h_max])
w_max_list = set([i for i, v in enumerate(w_list) if v == w_max])

place = len(h_max_list) * len(w_max_list)
candidate = 0
for h,w in target:
  if (h in h_max_list) and (w in w_max_list):
    candidate +=1

print(h_max+w_max-(place == candidate))

Solution (Python):
H,W,M = map(int, input().split())

hs = [0] * H
ws = [0] * W
s = set()

for i in range(M):
  h,w = map(int, input().split())
  h -= 1
  w -= 1
  hs[h] += 1
  ws[w] += 1
  s.add((h,w))

mh = 0
mw = 0
for i in range(H):
  mh = max(mh,hs[i])
for j in range(W):
  mw = max(mw,ws[j])
i_s = list()
j_s = list()
for i in range(H):
  if mh == hs[i]:
    i_s.append(i)
for j in range(W):
  if mw == ws[j]:
    j_s.append(j)
ans = mh + mw
for i in i_s:
  for j in j_s:
    if (i,j) in s:
      continue
    print(ans)
    exit()
ans -= 1
print(ans)

Solution (Python):
H,W,M = map(int,input().split())
hs = [0] * H
ws = [0] * W
s = set()

for i in range(M):
  r,c = map(int,input().split())
  r -= 1
  c -= 1
  hs[r] += 1
  ws[c] += 1
  s.add((r,c))
mh = 0
mw = 0
for i in range(H):
  mh = max(mh, hs[i])
for j in range(W):
  mw = max(mw, ws[j])

i_s = list()
j_s = list()
for i in range(H):
  if mh == hs[i]:
    i_s.append(i)
for j in range(W):
  if mw == ws[j]:
    j_s.append(j)
ans = mh + mw
for i in i_s:
  for j in j_s:
    if (i,j) in s:
      continue
    print(ans)
    exit()
ans -= 1
print(ans)

Solution (Python):

n,m,t=map(int,input().split())

grid=[]

row=[0]*n
col=[0]*m

for _ in range(t):
    x,y=map(int,input().split())
    grid.append((x-1,y-1))
    row[x-1]+=1
    col[y-1]+=1

prow=[]
pcol=[]
qr=max(row)
for i in range(n):
    if row[i]==qr :
        prow.append(i)

qc=max(col)
for i in range(m):
    if col[i]==qc :
        pcol.append(i)

num=len(prow)*len(pcol)
ans=qr+qc
for i in grid:
    if row[i[0]]==qr and col[i[1]]==qc :
        num-=1
if num>0:
    print(ans)
else:
    print(ans-1)

Solution (Python):
import sys
from collections import deque
H, W, M = map(int, input().split())
S = list(tuple(map(int, input().split())) for _ in range(M))
hh = dict()
ww = dict()
for i in range(M):
    h, w = S[i]
    if h in hh.keys():
        hh[h] += 1
    else:
        hh[h] = 1
    if w in ww.keys():
        ww[w] += 1
    else:
        ww[w] = 1
hhh = sorted(hh.items(), key=lambda x:x[1], reverse=True)
www = sorted(ww.items(), key=lambda x:x[1], reverse=True)
h = hhh[0][1]
w = www[0][1]
ans = h+w
Nh = 0
Nw = 0
for i in range(len(hhh)):
    if h == hhh[i][1]:
        Nh += 1
    else:
        break
for i in range(len(www)):
    if w == www[i][1]:
        Nw += 1
    else:
        break
tt = Nh*Nw
if tt > M:
    print(ans)
    sys.exit()

for tmp in S:
    if hh[tmp[0]] == h and ww[tmp[1]] == w:
        tt -= 1
if tt > 0:
    print(ans)
else:
    print(ans-1)

Solution (Python):
def main():
    H, W, M = map(int, input().split())
    hw = [tuple(list(map(int, input().split()))) for _ in [0]*M]
    hw_set = set(hw)
    h_count = [0 for i in range(H+1)]
    w_count = [0 for i in range(W+1)]
    for h, w in hw:
        h_count[h] += 1
        w_count[w] += 1
    h_m = max(h_count)
    w_m = max(w_count)
    h_list = []
    w_list = []
    for i in range(H+1):
        if h_count[i] == h_m:
            h_list.append(i)
    for i in range(W+1):
        if w_count[i] == w_m:
            w_list.append(i)
    
    for i in h_list:
        for j in w_list:
            if (i, j) not in hw_set:
                print(h_m+w_m)
                return
    print(h_m+w_m-1)


main()

Solution (Python):
H, W, M = map(int, input().split())
row=[0]*H 
col=[0]*W 
 
mat=[] 
 
for i in range(M):
    h, w = map(int, input().split())
    row[h-1]+=1
    col[w-1]+=1
    mat.append((h-1,w-1))
r=max(row)
c=max(col)
 
rr=[1 if row[i]==r else 0 for i in range(H)] 
cc=[1 if col[i]==c else 0 for i in range(W)] 
 
x=0 
for k in mat:
    if rr[k[0]]==1 and cc[k[1]]==1:
        x+=1
        
if sum(rr)*sum(cc)==x: 
    print(r+c-1)
else:
    print(r+c)

Solution (Python):
h,w,m=map(int,input().split())

H,W=[0]*h,[0]*w
P=set()

for i in range(m):
  y,x=map(int,input().split())
  H[~-y]+=1
  W[~-x]+=1
  P.add(~-y*w+~-x)

mh,mw=max(H),max(W)
MH=[i for i,h in enumerate(H) if h==mh]
MW=[i for i,v in enumerate(W) if v==mw]
for y in MH:
  for x in MW:
    if y*w+x not in P:
      print(mh+mw)
      exit()

print(mh+mw-1)

Solution (Python):


from collections import Counter

h, w, m = map(int, input().split())
hw = [tuple(map(int, input().split())) for i in range(m)]

ys, xs = zip(*hw)
ymax, = Counter(ys).most_common(1)
xmax, = Counter(xs).most_common(1)

bombed = max(ymax[1], xmax[1])

if bombed < m:
    if ymax[1] > xmax[1]:
        xs = [b for a, b in hw if a != ymax[0]]
        xmax, = Counter(xs).most_common(1)
        bombed += xmax[1]
    else:
        ys = [a for a, b in hw if b != xmax[0]]
        ymax, = Counter(ys).most_common(1)
        bombed += ymax[1]

print(bombed)

Solution (Python):
H, W, M = map(int, input().split())
row=[0]*H
col=[0]*W

mat=[]

for i in range(M):
  h, w = map(int, input().split())
  row[h-1]+=1
  col[w-1]+=1
  mat.append((h-1,w-1))
  
r=max(row)
c=max(col)

rr=[1 if row[i]==r else 0 for i in range(H)]
cc=[1 if col[i]==c else 0 for i in range(W)]

x=0
for k in mat:
  if rr[k[0]]==1 and cc[k[1]]==1:
    x+=1

if sum(rr)*sum(cc)==x:
  print(r+c-1)
else:
  print(r+c)

Solution (Python):


from collections import Counter

h, w, m = map(int, input().split())
hw = [tuple(map(int, input().split())) for i in range(m)]

ys, xs = zip(*hw)
ymax, = Counter(ys).most_common(1)
xmax, = Counter(xs).most_common(1)

bombed = max(ymax[1], xmax[1])

if bombed < m:
    if ymax[1] > xmax[1]:
        hw2 = [(a, b) for a, b in hw if a != ymax[0]]
        _, xs = zip(*hw2)
        xmax, = Counter(xs).most_common(1)
        bombed += xmax[1]
    else:
        hw2 = [(a, b) for a, b in hw if b != xmax[0]]
        ys, _ = zip(*hw2)
        ymax, = Counter(ys).most_common(1)
        bombed += ymax[1]

print(bombed)

Solution (Python):
import numpy as np
import numba
import sys
from numba import njit, b1, i4, i8, f8

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


@njit((i8, i8, i8[:]), cache=True)
def main(Height, Width, XY):
    UP = 300010
    row = np.zeros(UP, np.int64)
    col = np.zeros(UP, np.int64)
    
    for i in range(0, len(XY), 2):
        x, y = XY[i:i + 2] - 1
        row[x] += 1
        col[y] += 1
    
    row_max = row.max()
    col_max = col.max()

    judge = np.sum(row == row_max) * np.sum(col == col_max)

    for i in range(0, len(XY), 2):
        x, y = XY[i:i + 2] - 1
        
        if (row[x] == row_max and col[y] == col_max):
            judge -= 1
    
    summ = row_max + col_max
    
    return (summ if judge else summ - 1)


Height, Width, M_ko = map(int, readline().split())
XY = np.array(read().split(), np.int64)

ans = main(Height, Width, XY)
print(ans)

Solution (Python):
h, w, m = map(int, input().split())
r = [0] * (h+1)
c = [0] * (w+1)
bomb = []
for _ in range(m):
    hi, wi = map(int, input().split())
    r[hi] += 1
    c[wi] += 1
    bomb.append([hi, wi])

hmax = max(r)
wmax = max(c)

cnt = 0
ret = 1
for i in range(1, h+1):
    if r[i] == hmax:
        cnt += 1
ret *= cnt

cnt = 0
for i in range(1, w+1):
    if c[i] == wmax:
        cnt += 1

ret *= cnt

for i in range(m):
    hi, wi = bomb[i]
    if r[hi] == hmax and c[wi] == wmax:
        ret -= 1

if ret == 0:
    print(hmax + wmax - 1)
else:
    print(hmax + wmax)

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

def main():
    H, W, M = LI()
    hw = LIR(M)

    s = set(h + w * (10 ** 6) for h, w in hw)

    ch = Counter(i for i, _ in hw)
    cw = Counter(j for _, j in hw)
    _, maxcy = ch.most_common(1)[0]
    _, maxcx = cw.most_common(1)[0]

    ans = maxcy + maxcx - 1

    lh = [k for k, v in ch.items() if v == maxcy]
    lw = [k for k, v in cw.items() if v == maxcx]

    for h in lh:
        for w in lw:
            if not h + w * (10 ** 6) in s:
                print(ans + 1)
                return
    print(ans)




if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    H, W, M = map(int, readline().split())
    row = [0] * H
    column = [0] * W
    grid = set()

    for _ in range(M):
        h, w = map(int, readline().split())
        row[h-1] += 1
        column[w-1] += 1
        grid.add((h-1,w-1))

    max_row = max(row)
    max_column = max(column)

    idx_row = [i for i,x in enumerate(row) if x == max_row]
    idx_column = [i for i,x in enumerate(column) if x == max_column]

    ans = max_row+max_column
    flag = False

    for h in idx_row:
        for w in idx_column:
            if not (h,w) in grid:
                flag = True
                break

    if flag:
        print(ans)
    else:
        print(ans-1)


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import chain
from bisect import bisect_left, bisect_right

H, W, M = map(int, input().split())
YX = [list(map(int, input().split())) for _ in range(M)]



X_sets = [[] for _ in range(W+1)]
Y_sets = [[] for _ in range(H+1)]
for y, x in YX:
    X_sets[x].append(y)
    Y_sets[y].append(x)

[s.sort() for s in X_sets]
[s.sort() for s in Y_sets]



X_set_max_len = max(map(len, X_sets))
Y_set_max_len = max(map(len, Y_sets))






X_list = set(i for i, s in enumerate(X_sets) if len(s) == X_set_max_len)
Y_list = set(i for i, s in enumerate(Y_sets) if len(s) == Y_set_max_len)

score = X_set_max_len + Y_set_max_len - 1

for x in X_list:
    for y in Y_list:
        c = bisect_right(X_sets[x], y) - bisect_left(X_sets[x], y)
        
        if c == 0:
            score += 1
            print(score)
            exit()

print(score)

Solution (Python):
H,W,M = map(int, input().split())
width = [0]*W
height = [0]*H

st = set()
for i in range(M):
  y, x = map(int, input().split())
  st.add((x - 1,y - 1))
  width[x-1] += 1
  height[y-1] += 1

maxW = max(width)
maxH = max(height)

indexW = [i for i, v in enumerate(width) if v == maxW]
indexH = [i for i, v in enumerate(height) if v == maxH]

ans = maxW + maxH 


tmpX = indexW[0]
tmpY = indexH[0]


flag = 0





for x in indexW:
  y = tmpY
  if not (x, y) in st:
    flag = 1
    break


for y in indexH:
  x = tmpX
  if not (x, y) in st:
    flag = 1
    break

if flag == 1:
  print(ans)
else:
  print(ans - 1)

Solution (Python):
h,w,m = map(int,input().split())
row = [0]*(h+1)
col = [0]*(w+1)
bombs = set([])
for i in range(m):
  a,b = map(int,input().split())
  row[a] += 1
  col[b] += 1
  bombs.add((a,b))
r,c = max(row),max(col)
rcnt,ccnt = 0,0
for v in row:
  if v==r:
    rcnt += 1
for v in col:
  if v==c:
    ccnt += 1
doubled = 0
for i,j in bombs:
  if row[i]==r and col[j]==c:
    doubled += 1
if doubled==rcnt*ccnt:
  print(r+c-1)
else:
  print(r+c)

Solution (Python):
h,w,m = map(int,input().split())
row = [0]*(h+1)
col = [0]*(w+1)
bombs = set([])
for i in range(m):
  a,b = map(int,input().split())
  row[a] += 1
  col[b] += 1
  bombs.add((a,b))
r,c = max(row),max(col)
rcnt,ccnt = 0,0
for v in row:
  if v==r:
    rcnt += 1
for v in col:
  if v==c:
    ccnt += 1
doubled = 0
for i,j in bombs:
  if row[i]==r and col[j]==c:
    doubled += 1
if doubled==rcnt*ccnt:
  print(r+c-1)
else:
  print(r+c)

Solution (Python):
H, W, M = map(int, input().split())
bombs = {}
rows = []
cols = []
for i in range(H):
    rows.append([i, 0])
for i in range(W):
    cols.append([i, 0])
for i in range(M):
    y, x = map(lambda x: x - 1, map(int, input().split()))
    bombs[(y,x)] = True
    rows[y][1] += 1
    cols[x][1] += 1


rows = sorted(rows, key=lambda x: x[1], reverse=True)
cols = sorted(cols, key=lambda x: x[1], reverse=True)
answer = 0
for row in rows:
    a = cols[0][1] + row[1] - (1 if (row[0], cols[0][0]) in bombs else 0)
    if a > answer:
        answer = a
for col in cols:
    a = rows[0][1] + col[1] - (1 if (rows[0][0], col[0]) in bombs else 0)
    if a > answer:
        answer = a
print(answer)

Solution (Python):
H, W, M = map(int,input().split())
Bomb = []
for _ in range(M):
    hi, wi = map(lambda x: int(x) - 1, input().split())
    Bomb.append((hi, wi))

counterH = [0] * H
counterW = [0] * W

for (hi, wi) in Bomb:
    counterH[hi] += 1
    counterW[wi] += 1


max_h = max(counterH)
max_w = max(counterW)

count = 0
for (hi, wi) in Bomb:
    if counterH[hi] == max_h and counterW[wi] == max_w:
        count += 1


ans = max_h + max_w
if count == counterH.count(max_h) * counterW.count(max_w):
    ans -= 1
print(ans)

Solution (Python):
h, w, m = map(int, input().split())
b = []
for i in range(m):
    hh, ww = map(int,input().split())
    hh -= 1
    ww -= 1
    b.append([hh, ww])
    
cnth = [0] * h
cntw = [0] * w
          
for i in range(m):
    hh, ww = b[i][0], b[i][1]
    cnth[hh] += 1
    cntw[ww] += 1
    
mh = max(cnth)
mw = max(cntw)

cnt_mh = 0
for i in range(h):
    if cnth[i] == mh:
        cnt_mh += 1
cnt_mw = 0
for i in range(w):
    if cntw[i] == mw:
        cnt_mw += 1

cnt_m = 0
for i in range(m):
    hh, ww = b[i][0], b[i][1]
    if cnth[hh] == mh and cntw[ww] == mw:
        cnt_m += 1
print(mh+mw) if cnt_mh*cnt_mw > cnt_m else print(mh+mw-1)

Solution (Python):
H, W, M = map(int, input().split())

Hc = [0 for i in range(H)]
Wc = [0 for i in range(W)]
B = []

for m in range(M):
  h, w = map(int, input().split())
  Hc[h - 1] += 1
  Wc[w - 1] += 1
  B.append((h - 1, w - 1))
Hcmax = max(Hc)
Hcmax_idx = [i for i, x in enumerate(Hc) if x == Hcmax] 
Wcmax = max(Wc)
Wcmax_idx = [i for i, x in enumerate(Wc) if x == Wcmax] 

ans = Hcmax + Wcmax - 1
B = set(B)
f = False
for i in Hcmax_idx:
  if f:
    break
  for j in Wcmax_idx:
    if (i, j) not in B:
      ans = Hcmax + Wcmax
      f = True
      break
print(ans)

Solution (Python):
from collections import defaultdict
h, w, m = map(int, input().split())
target = []
point = defaultdict(int)
count_target_of_y = defaultdict(int)
count_target_of_x = defaultdict(int)
for i in range(m):
    x, y = map(int, input().split())
    count_target_of_y[y] += 1
    count_target_of_x[x] += 1
    point[(x,y)] += 1
max_x_count = max(list(count_target_of_x.items()), key=lambda x:x[1])[1]
max_y_count = max(list(count_target_of_y.items()), key=lambda x:x[1])[1]
max_x_count_list = [i for i in count_target_of_x.items() if i[1] == max_x_count]
max_y_count_list = [i for i in count_target_of_y.items() if i[1] == max_y_count]
for x in max_x_count_list:
    for y in max_y_count_list:
        if point[(x[0], y[0])] == 0:
            print(max_x_count + max_y_count)
            exit()
print(max_x_count + max_y_count - 1)

Solution (Python):
def solve():
    H, W, M = map(int, input().split())
    N = 3 * 10 ** 5
    cnth = [0] * N
    cntw = [0] * N
    st = set()
    for i in range(M):
        h, w = map(lambda x: int(x) - 1, input().split())
        cnth[h] += 1
        cntw[w] += 1
        st.add((h, w))
    mh = max(cnth)
    mw = max(cntw)
    Y = []
    X = []
    for i in range(H):
        if cnth[i] == mh:
            Y.append(i)
    for i in range(W):
        if cntw[i] == mw:
            X.append(i)
    for y in Y:
        for x in X:
            if not (y, x) in st:
                return mh + mw
    return mh + mw - 1

print(solve())

Solution (Python):
def solve():
    H, W, M = map(int, input().split())
    N = 3 * 10 ** 5
    cnth = [0] * N
    cntw = [0] * N
    dic = dict()
    for i in range(M):
        h, w = map(lambda x: int(x) - 1, input().split())
        cnth[h] += 1
        cntw[w] += 1
        dic[(h, w)] = 1
    mh = max(cnth)
    mw = max(cntw)
    Y = []
    X = []
    for i in range(H):
        if cnth[i] == mh:
            Y.append(i)
    for i in range(W):
        if cntw[i] == mw:
            X.append(i)
    for y in Y:
        for x in X:
            if not (y, x) in dic:
                return mh + mw
    return mh + mw - 1

print(solve())

Solution (Python):

H, W, M = list(map(int, input().split()))

h = [0] * H
w = [0] * W

hw = {}

for _ in range(M):
    a, b = list(map(int, input().split()))
    h[a - 1] += 1
    w[b - 1] += 1
    temp = "x" + str(a - 1) + "y" + str(b - 1)
    hw[temp] = 1

hm = max(h)
wm = max(w)

hp = [i for i, v in enumerate(h) if v == hm]
wp = [i for i, v in enumerate(w) if v == wm]

for x in hp:
    for y in wp:
        temp = "x" + str(x) + "y" + str(y)
        if temp not in hw:
            print(hm + wm)
            exit()
else:
    print(hm + wm - 1)

Solution (Python):
h, w, m = map(int, input().split())
bord_x, bord_y = [0]*h, [0]*w
 
hw = []
for _i in range(m):
    s, t = map(int, input().split())
    s -= 1
    t -= 1
    bord_x[s] += 1
    bord_y[t] += 1
    hw.append([s, t])
 
x_max, y_max = max(bord_x), max(bord_y)
 
cnt = 0
for i, j in hw:
    if bord_x[i] == x_max and bord_y[j] == y_max:
        cnt += 1
 
if cnt == bord_x.count(x_max)*bord_y.count(y_max):
    x_max -= 1
 
print(x_max+y_max)

Solution (Python):
def main():
    h, w, m, *BOM = map(int, open(0).read().split())
    row = [0] * h
    col = [0] * w
    for i, j in zip(BOM[::2], BOM[1::2]):
        i -= 1
        j -= 1
        row[i] += 1
        col[j] += 1

    p, q = max(row), max(col)
    rr = sum(1 for i in range(h) if row[i] == p)
    rl = sum(1 for i in range(w) if col[i] == q)

    cnd = rr * rl

    for i, j in zip(BOM[::2], BOM[1::2]):
        i -= 1
        j -= 1
        if (row[i], col[j]) == (p, q):
            cnd -= 1

    if cnd > 0:
        ans = p + q
    else:
        ans = p + q - 1

    print(ans)


if __name__ == '__main__':
    main()