#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(n) n.begin(), n.end()
#define el '\n'
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
const int N = 1e6 + 5;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
vector<string> arr(n);
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
bool con = true;
for (int i = 0; i < n && con; i++)
{
for (int j = 0; j < m; j++)
{
if (arr[i][j] == '1')
{
if (j == 0 || (i == 0 && j >= 0 && j < m))
{
continue;
}
else if (i - 1 >= 0 && j - 1 >= 0)
{
if (arr[i - 1][j] != '1' && arr[i][j - 1] != '1')
{
con = false;
break;
}
}
else
{
con = false;
break;
}
}
}
}
if (con)
cout << "YES\n";
else
cout << "NO\n";
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgYWxsKG4pIG4uYmVnaW4oKSwgbi5lbmQoKQojZGVmaW5lIGVsICdcbicKY29uc3QgaW50IGR4W10gPSB7MCwgMCwgMSwgLTF9Owpjb25zdCBpbnQgZHlbXSA9IHsxLCAtMSwgMCwgMH07CmNvbnN0IGludCBOID0gMWU2ICsgNTsKCmludCBtYWluKCkKewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShudWxscHRyKTsKCiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwoKICAgIHdoaWxlICh0LS0pCiAgICB7CiAgICAgICAgaW50IG4sIG07CiAgICAgICAgY2luID4+IG4gPj4gbTsKICAgICAgICB2ZWN0b3I8c3RyaW5nPiBhcnIobik7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgewogICAgICAgICAgICBjaW4gPj4gYXJyW2ldOwogICAgICAgIH0KICAgICAgICBib29sIGNvbiA9IHRydWU7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuICYmIGNvbjsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChhcnJbaV1bal0gPT0gJzEnKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGlmIChqID09IDAgfHwgKGkgPT0gMCAmJiBqID49IDAgJiYgaiA8IG0pKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKGkgLSAxID49IDAgJiYgaiAtIDEgPj0gMCkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChhcnJbaSAtIDFdW2pdICE9ICcxJyAmJiBhcnJbaV1baiAtIDFdICE9ICcxJykKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgY29uID0gZmFsc2U7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBjb24gPSBmYWxzZTsKICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmIChjb24pCiAgICAgICAgICAgIGNvdXQgPDwgIllFU1xuIjsKICAgICAgICBlbHNlCiAgICAgICAgICAgIGNvdXQgPDwgIk5PXG4iOwogICAgfQp9Cg==