文件目录

// 本题的k就是x
// 状态:dp[i][j][k] 走到ij位置,改变了k个?的最大分值
// 方程:dp[ijk]=max(上,左)+分
#include <bits/stdc++.h>
using namespace std;
int t, n, m, k;
char g[505][505];
int dp[505][505][305];
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n >> m >> k;
        memset(g, 0, sizeof(g));
        memset(dp, 0, sizeof(dp));
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                cin >> g[i][j];
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                for (int x = 0; x <= k; x++)
                {
                    dp[i][j][x] = max(dp[i][j - 1][x], dp[i - 1][j][x]) + (g[i][j] == '1' ? 1 : 0);
                    if (g[i][j] == '?' && x > 0)
                        dp[i][j][x] = max(dp[i][j][x], max(dp[i][j - 1][x - 1], dp[i - 1][j][x - 1]) + 1);
                }
            }
        }
        for (int x = 0; x <= k; x++)
            ans = max(ans, dp[n][m][x]);
        cout << ans << "\n";
    }
    return 0;
}