Avenger Formation

0

0 votes
Very-Easy
Problem

All the avengers have gathered at the shield headquarters and are standing randomly on a grid of N*M cells . The avengers are divided into two groups , Novices and Legends . The legends have a strength value of 1 and the novices have a strength value of 0 . The strength of any novice is increased to 1 if he is given a booster , but for one battle only . A novice can be given only 1 booster per battle. A battle is described as by the following rules :-

  • . Any cell from the grid can be chosen for battle .

  • . All the avengers in the same row and same column as the chosen cell , will take part in the battle .

  • . The selected avengers are divided into two teams , one comprising of all the novices and the other comprising of all the legends .

  • . The team with the higher cumulative strength value (i.e sum of strength of all members of the team ) wins .

  • . In case that the strength on both teams is the same , the team comprising of the legends win .

  • . A team must have the total strength more than 0 to win any battle .

The legends are no doubt superior in strength than the novices , so the novices want your help in winning their battles . For any cell (X,Y) , you need to tell them how many boosters are required by the novices to win the battle for that cell (X,Y) .You need to do this for all cells from 1,1 to N,N .

Input

First line contains an integer T denoting the number of test cases. Next line contains 2 integers N and M denoting the numer of rows and columns. Each of the next N lines contains M integer. Each integer is either 0 or 1 , with 0 meaning that a novice is standing in the cell and 1 meaning that a legend is standing in that cell.

Output

You have to give the output in the form of a similar grid , N lines each containg M space separated integers . The value of the Yth integer of the Xth row must be the number of boosters required by the novices to win the battle if cell (X,Y) is chosen for battle . In the case that the novices cannot win the battle for cell (X,Y) , print -1 .

Constraints

1 <= T <= 10

1 <= N <= 1000

1 <= M <= 1000

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There is a 2*2 grid , when cell 1,1 is chosen , 2 novices and 1 legend participates in the battle . Therefore 2 boosters are required by the novices . Similarly for 1,2 and 2,1 , 2 boosters are required . For cell 2,2 , 3 novices participate , so to have a total strength of atleast 1 , 1 booster will be required , hence output .

Editor Image

?