← Home
Looking at this problem, I need to determine when a ball configuration is achievable through pushes from the left or top.

**Key Observation:**
For a ball to end up at position (i, j):
- If pushed from the left into row i, all positions (i, 0), (i, 1), ..., (i, j-1) must have been occupied at that time (so the new ball "pushes through" to position j)
- If pushed from the top into column j, all positions (0, j), (1, j), ..., (i-1, j) must have been occupied

This leads to the necessary condition: For every ball at (i, j), either all positions to its left in the same row are filled, OR all positions above it in the same column are filled.

**Condition:** For a ball at position (i, j):
- Let L = count of balls in row i at columns 0 to j-1
- Let U = count of balls in column j at rows 0 to i-1
- Must have: L = j OR U = i

**Verification with Example 5 (NO case):**
```
000
000
001
```
Ball at (2,2): L = 0 (no balls to left), U = 0 (no balls above)
j = 2, i = 2
Neither L = j (0 ≠ 2) nor U = i (0 ≠ 2) ✓→ Condition fails → NO

**Verification with Example 1 (YES case):**
- Ball (0,2): U = 0 = i (row 0) ✓
- Ball (1,2): U = 1 = i ✓
- Ball (2,0): L = 0 = j ✓
- Ball (2,1): L = 1 = j ✓
All pass → YES

```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var t int
	fmt.Fscan(reader, &t)

	for tc := 0; tc < t; tc++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)

		grid := make([]string, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &grid[i])
		}

		rowPrefix := make([][]int, n)
		for i := 0; i < n; i++ {
			rowPrefix[i] = make([]int, m+1)
			for j := 0; j < m; j++ {
				rowPrefix[i][j+1] = rowPrefix[i][j]
				if grid[i][j] == '1' {
					rowPrefix[i][j+1]++
				}
			}
		}

		colPrefix := make([][]int, m)
		for j := 0; j < m; j++ {
			colPrefix[j] = make([]int, n+1)
			for i := 0; i < n; i++ {
				colPrefix[j][i+1] = colPrefix[j][i]
				if grid[i][j] == '1' {
					colPrefix[j][i+1]++
				}
			}
		}

		ok := true
		for i := 0; i < n && ok; i++ {
			for j := 0; j < m && ok; j++ {
				if grid[i][j] == '1' {
					L := rowPrefix[i][j]
					U := colPrefix[j][i]
					if L != j && U != i {
						ok = false
					}
				}
			}
		}

		if ok {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}
```