Juno hates birds!!

Time limit2sMemory limit256 MB

Summary
Given an n by m grid of numbers, remove the row or column containing the most digit 9s (ties broken by scanning order) and count the remaining 9s.
Level

Medium4 of 10

Topics
Array, Implementation
Solved
No attempts yet

Problem

Juno hates birds, and he hates pigeons most of all.

During class Juno and the classmate sitting next to him decided to play bingo. Each of them wrote the numbers they wanted on an n×mn \times m bingo board, and then they swapped boards. As soon as Juno looked at his partner's board he got angry, because so many of the numbers contained the digit 9 that he started thinking about pigeons. So he decided to smash the board.

His rampage follows one rule. Among all rows and all columns, he picks exactly one row or column that holds the digit 9 the most times, and smashes every cell in it.

Nines are counted digit by digit, not cell by cell. A cell holding 999 has three nines, and a cell holding 90 has one.

The moment the board broke, the teacher looked straight at Juno and decided to hit him once for every 9 still on the board. How many times does Juno get hit?

Input

The first line contains the board size nn and mm (1≤n≤5001 \le n \le 500, 1≤m≤5001 \le m \le 500).

Each of the next nn lines contains mm numbers separated by spaces. Every number written on the board is a non-negative integer not greater than 10,000.

Output

Print the number of hits Juno takes, which is the number of nines left on the board after one row or one column is smashed.

Examples8

  1. Example 1

    Input
    3 4
    1 2 3 9
    4 5 9 6
    9 7 8 9
    
    Expected output
    2
    
  2. Example 2

    Input
    4 4
    11 12 19 14
    99 39 14 90
    13 47 81 99
    32 72 29 66
    
    Expected output
    4
    
  3. Example 3

    Input
    1 1
    9
    
    Expected output
    0
    
  4. Example 4

    Input
    1 1
    0
    
    Expected output
    0
    
  5. Example 5

    Input
    1 5
    10000 1234 5678 0 100
    
    Expected output
    0
    
  6. Example 6

    Input
    2 2
    9999 9999
    9999 9999
    
    Expected output
    8
    
  7. Example 7

    Input
    4 3
    9 1 2
    9 3 4
    9 5 6
    9 7 8
    
    Expected output
    0
    
  8. Example 8

    Input
    3 3
    999 1 1
    1 9 1
    1 1 99
    
    Expected output
    3