#K51432. Captain America's Strike

    ID: 29086 Type: Default 1000ms 256MiB

Captain America's Strike

Captain America's Strike

Captain America is on a mission to take down as many HYDRA soldiers with a single throw as possible. The battlefield is represented as a matrix, where:

  • 1 represents a HYDRA soldier.
  • 0 represents an empty space.
  • -1 represents an obstacle that blocks his path.

During his throw, he can hit a series of contiguous HYDRA soldiers in either a horizontal (row-wise) or vertical (column-wise) line. However, if there is an obstacle (-1) in his path, his hit streak resets, and he cannot continue hitting beyond that point. Your task is to determine the maximum number of HYDRA soldiers he can destroy in one throw.

The key idea is to scan the battlefield matrix for contiguous sequences of 1's in both the horizontal and vertical directions. Every time an obstacle (-1) or an empty cell (0) is encountered, the count resets.

The solution must read input from stdin and write output to stdout.

inputFormat

The input starts with an integer T representing the number of test cases. For each test case, the first line contains two integers N and M which represent the number of rows and columns of the battlefield respectively. This is followed by N lines, each containing M space-separated integers that represent the battlefield matrix.

outputFormat

For each test case, output a single integer on a new line – the maximum number of HYDRA soldiers Captain America can destroy with one throw.

## sample
1
4 4
0 1 1 0
1 -1 1 0
1 1 1 0
0 0 0 0
3

</p>