Capture Surrounded Regions

Medium

You are given a matrix board of size n*m, containing either 'X' or 'O' in each cell. 'X' represents cells you own, and 'O' represents those you don't.

You can capture regions that are surrounded by 'X' in all directions by turning each of their cells from 'O' to 'X'.

Note: Two cells are connected if they share a common edge.

You have to return the board configuration after you have captured as many cells as you can.

Example:
capture-surrounded-regions

Testing

Input Format

The first line contains an integer ‘T’ denoting the number of test cases.

For each test case the input has the following lines:

  • Two space-separated integers 'n' and 'm' denoting the number of rows and columns of the matrix.
  • n lines, each containing m space-separated characters denoting the elements of the matrix.
Output Format

For each test case, the output has n lines, each containing m space-separated characters denoting the final state of the board.

Sample Input

2
4 5
X X X X X
X X O O X
X O X O X
X O X X X
3 3
X X X
X O X
X X X

Expected Output

X X X X X 
X X X X X
X O X X X
X O X X X
X X X
X X X
X X X

Constraints

1 <= T <= 10
1 <= n, m <= 200
boardij = 'X' or 'O'

Companies
Editorial Link: Editorial