Two Hands, One V
Time limit2sMemory limit1024 MB
Given a binary grid, find the maximum number of blue cells after two V-shaped paintings, each following an upper-left and upper-right diagonal from a white cell.
- Level
Medium6 of 10
- Topics
- Matrix, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
Each cell of an -row, -column grid is painted either white or black.
An action called "V-shaped painting" is defined as follows:
- Choose one white cell.
- Starting from the chosen cell, move along the upper-left diagonal and paint every cell you pass blue, until you reach a cell that is not white or leave the grid.
- Starting from the cell one step to the upper right of the chosen cell, move along the upper-right diagonal and paint every cell you pass blue, until you reach a cell that is not white or leave the grid.
Find the maximum number of blue cells possible after performing "V-shaped painting" twice.
For example, suppose the following grid is given.

After one V-shaped painting at row 5, column 5, the grid looks like this.

Then a V-shaped painting at row 5, column 9 paints 11 cells blue in total.

By contrast, a V-shaped painting at row 5, column 9 followed by one at row 5, column 5 paints 13 cells blue in total.

No two V-shaped paintings can color more than 13 cells blue, so the answer for this grid is 13.
Input
The first line contains and , separated by a space.
The next lines describe the grid. Each line is a string of length made of and . If the -th character of the -th line is , the cell at row , column is white. If it is , the cell is black. ()
Output
Print the maximum number of blue cells that can be obtained on the first line.
Constraints
- The grid contains at least 2 white cells.