stored
Codeforces D
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1010;
char s[N][N];
char g[N][N];
char ans[4] = {'1', '5', '4', '3'};
int solve()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
scanf("%s", s[i]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
g[i][j] = s[i - 1][j - 1];
int x = n, y = m;
int st = 1;
int count = 0;
while (x && y)
{
int i = st, j = st;
int idx = 0;
for (; j <= st + y - 1; j++)
{
// printf("g[i][j]: %c ans[idx]: %c\n", g[i][j], ans[idx]);
if (g[i][j] == ans[idx])
idx++;
else idx = 0;
if (idx == 4)
{
idx = 0;
count++;
}
}
j--;
for (; i <= st + x - 1; i++)
{
// printf("g[i][j]: %c ans[idx]: %c\n", g[i][j], ans[idx]);
if (g[i][j] == ans[idx])
idx++;
else idx = 0;
if (idx == 4)
{
idx = 0;
count++;
}
}
i--;
for (; j >= st; j--)
{
// printf("g[i][j]: %c ans[idx]: %c\n", g[i][j], ans[idx]);
if (g[i][j] == ans[idx])
idx++;
else idx = 0;
if (idx == 4)
{
idx = 0;
count++;
}
}
j++;
for (; i >= st; i--)
{
// printf("g[i][j]: %c ans[idx]: %c\n", g[i][j], ans[idx]);
if (g[i][j] == ans[idx])
idx++;
else idx = 0;
if (idx == 4)
{
idx = 0;
count++;
}
}
i++;
for (; j <= st + 2; j++)
{
// printf("g[i][j]: %c ans[idx]: %c\n", g[i][j], ans[idx]);
if (g[i][j] == ans[idx])
idx++;
else idx = 0;
if (idx == 4)
{
idx = 0;
count++;
}
}
x -= 2, y -= 2, st++;
}
return count;
}
int main()
{
int T; cin >> T;
while (T--)
{
cout << solve() << endl;
}
return 0;
}