------
## 最终结论
To solve the problem, we need to construct a grid of size `n x n` filled with integers `0` and `1` such that the sum of all elements is exactly `k`. The goal is to minimize the value `f(A)`, defined as:

\[ f(A) = (\max(R) - \min(R))^2 + (\max(C) - \min(C))^2 \]

where \( R_i \) are the row sums and \( C_j \) are the column sums.

Here's a step-by-step breakdown of how to approach this problem:

1. **Input Parsing**: First, read the number of test cases and for each test case, read the values of `n` and `k`.

2. **Grid Construction**:
   - Initialize an empty grid of size `n x n` filled with zeros.
   - Distribute `k` ones across the grid in a way that minimizes the difference between row sums and column sums. A good strategy is to fill diagonally, wrapping around when reaching the end of a row or column.

3. **Calculation of f(A)**:
   - After constructing the grid, calculate row sums (`R`) and column sums (`C`).
   - Compute `f(A)` using these sums.

4. **Output**: For each test case, output the minimum possible value of `f(A)` followed by the grid itself.

Below is the Python code implementing this solution:

```python
def read_input():
    import sys
    input = sys.stdin.read
    data = input().split()
    
    index = 0
    t = int(data[index])
    index += 1
    
    test_cases = []
    
    for _ in range(t):
        n = int(data[index])
        k = int(data[index + 1])
        test_cases.append((n, k))
        index += 2
        
    return test_cases

def construct_grid(n, k):
    grid = [[0] * n for _ in range(n)]
    
    r, c = 0, 0
    while k > 0:
        grid[r][c] = 1
        k -= 1
        r += 1
        c += 1
        if r == n:
            r = 0
            c -= (n - 1)
        if c == n:
            c = 0
            
    return grid

def calculate_f_A(grid):
    n = len(grid)
    row_sums = [sum(row) for row in grid]
    col_sums = [sum(grid[i][j] for i in range(n)) for j in range(n)]
    
    max_row_sum = max(row_sums)
    min_row_sum = min(row_sums)
    max_col_sum = max(col_sums)
    min_col_sum = min(col_sums)

    f_A = (max_row_sum - min_row_sum) ** 2 + (max_col_sum - min_col_sum) ** 2
    return f_A

def solve(test_cases):
    results = []
    
    for n, k in test_cases:
        grid = construct_grid(n, k)
        f_A_value = calculate_f_A(grid)
        
        results.append(str(f_A_value))
        for row in grid:
            results.append("".join(map(str, row)))
            
    return results

def main():
    test_cases = read_input()
    results = solve(test_cases)
    
    print("\n".join(results))

```

This code reads input data, constructs grids with minimal `f(A)` values by filling diagonally, calculates required metrics, and outputs them as specified.