using System.Collections.Generic;
public static void Main()
int t = int.Parse(Console.ReadLine());
var matrices = new List<Matrix>();
for(int i = 0; i < t; i++)
string[] line = Console.ReadLine().Split(' ');
int n = int.Parse(line[0]);
int m = int.Parse(line[1]);
int[,] matrix = new int[n, m];
for(int j = 0; j < n; j++)
string[] row = Console.ReadLine().Split(' ');
for(int k = 0; k < m; k++)
matrix[j, k] = int.Parse(row[k]);
var mat = new Matrix(matrix, n, m);
foreach(var matrix in matrices)
matrix.FindVerticalVertices();
matrix.FindDiagonalVertices();
int count = matrix.FindVerticalSquares() + matrix.FindDiagonalSquares();
Console.WriteLine(count);
public int[] Coordinates{get;set;}
public bool IsVertical{get;set;}
public int Quarter{get;set;}
public Vertex(int[] coordinates, bool isVertical, int quarter)
Coordinates = coordinates;
public static class VerticalTemplate
public static int[,] template1 = new int[3, 3]
public static int[,] template2 = new int[3, 3]
public static int[,] template3 = new int[3, 3]
public static int[,] template4 = new int[3, 3]
public static List<int[,]> Templates = new List<int[,]>{ template1, template2, template3, template4};
public static class DiagonalTemplate
public static int[,] template1 = new int[3, 3]
public static int[,] template2 = new int[3, 3]
public static int[,] template3 = new int[3, 3]
public static int[,] template4 = new int[3, 3]
public static List<int[,]> Templates = new List<int[,]>{ template1, template2, template3, template4};
public int[,] Mat{get;set;}
public List<Vertex>[] VerticalVertices{get;set;}
public List<Vertex>[] DiagonalVertices{get;set;}
public Matrix(int[,] matrix, int n, int m)
VerticalVertices = new List<Vertex>[4];
for(int i = 0; i < 4; i++)
VerticalVertices[i] = new List<Vertex>();
DiagonalVertices = new List<Vertex>[4];
for(int i = 0; i < 4; i++)
DiagonalVertices[i] = new List<Vertex>();
public int FindDiagonalSquares()
var possibleSouthNorthVertices = new List<Vertex[]>();
foreach(var vertex in DiagonalVertices[0])
foreach(var otherVertex in DiagonalVertices[2])
if(vertex.Coordinates[1] == otherVertex.Coordinates[1] && vertex.Coordinates[0] > otherVertex.Coordinates[0])
var vertexPair = new Vertex[]{vertex, otherVertex};
possibleSouthNorthVertices.Add(vertexPair);
var possibleWestEastVertices = new List<Vertex[]>();
foreach(var vertex in VerticalVertices[1])
foreach(var otherVertex in VerticalVertices[3])
if(vertex.Coordinates[0] == otherVertex.Coordinates[0] && vertex.Coordinates[1] < otherVertex.Coordinates[1])
var vertexPair = new Vertex[]{vertex, otherVertex};
possibleWestEastVertices.Add(vertexPair);
var possibleSquares = new List<Square>();
foreach(var southNorthPair in possibleSouthNorthVertices)
foreach(var westEastPair in possibleWestEastVertices)
if((southNorthPair[0].Coordinates[0] + southNorthPair[1].Coordinates[0]) / 2 == westEastPair[0].Coordinates[0]
&& ((westEastPair[1].Coordinates[1] + westEastPair[0].Coordinates[1]) / 2 == southNorthPair[0].Coordinates[1]))
var square = new Square(southNorthPair[0], westEastPair[0], southNorthPair[1], westEastPair[1]);
possibleSquares.Add(square);
var goodSquares = new List<Square>();
foreach(var square in possibleSquares)
var v0 = square.Vertices[0];
var v1 = square.Vertices[1];
var v2 = square.Vertices[2];
var v3 = square.Vertices[3];
for(int i = v0.Coordinates[0], j = v0.Coordinates[1]; i > v1.Coordinates[0] && j > v1.Coordinates[1]; i--, j--)
|| (j != 0 && Mat[i, j - 1] == 1)
|| (j != M - 1 && Mat[i, j + 1] == 1))
for(int j = v1.Coordinates[1]; j < v2.Coordinates[1]; j++)
int i = v1.Coordinates[0];
|| (i != 0 && Mat[i - 1, j] == 1)
|| (i != N - 1 && Mat[i + 1, j] == 1))
for(int i = v2.Coordinates[0]; i < v3.Coordinates[0]; i++)
int j = v2.Coordinates[1];
|| (j != 0 && Mat[i, j - 1] == 1)
|| (j != M - 1 && Mat[i, j + 1] == 1))
for(int j = v3.Coordinates[1]; j > v0.Coordinates[1]; j--)
int i = v3.Coordinates[1];
|| (i != 0 && Mat[i - 1, j] == 1)
|| (i != M - 1 && Mat[i + 1, j] == 1))
return goodSquares.Count;
public int FindVerticalSquares()
var possibleLeftEdges = new List<Vertex[]>();
foreach(var vertex in VerticalVertices[0])
foreach(var otherVertex in VerticalVertices[1])
if(vertex.Coordinates[1] == otherVertex.Coordinates[1] && vertex.Coordinates[0] > otherVertex.Coordinates[0])
var vertexPair = new Vertex[]{vertex, otherVertex};
possibleLeftEdges.Add(vertexPair);
var possibleRightEdges = new List<Vertex[]>();
foreach(var vertex in VerticalVertices[2])
foreach(var otherVertex in VerticalVertices[3])
if(vertex.Coordinates[1] == otherVertex.Coordinates[1] && vertex.Coordinates[0] < otherVertex.Coordinates[0])
var vertexPair = new Vertex[]{vertex, otherVertex};
possibleRightEdges.Add(vertexPair);
var possibleSquares = new List<Square>();
foreach(var leftPair in possibleLeftEdges)
foreach(var rightPair in possibleRightEdges)
if(leftPair[0].Coordinates[0] == rightPair[1].Coordinates[0] && leftPair[1].Coordinates[0] == rightPair[0].Coordinates[0]
&& Math.Abs(leftPair[0].Coordinates[0] - leftPair[1].Coordinates[0]) == Math.Abs(leftPair[0].Coordinates[1] - rightPair[1].Coordinates[1]))
var square = new Square(leftPair[0], leftPair[1], rightPair[0], rightPair[1]);
possibleSquares.Add(square);
var goodSquares = new List<Square>();
foreach(var square in possibleSquares)
var v0 = square.Vertices[0];
var v1 = square.Vertices[1];
var v2 = square.Vertices[2];
var v3 = square.Vertices[3];
for(int i = v0.Coordinates[0]; i > v1.Coordinates[0]; i--)
int j = v0.Coordinates[1];
|| (j != 0 && Mat[i, j - 1] == 1)
|| (j != M - 1 && Mat[i, j + 1] == 1))
for(int j = v1.Coordinates[1]; j < v2.Coordinates[1]; j++)
int i = v1.Coordinates[0];
|| (i != 0 && Mat[i - 1, j] == 1)
|| (i != N - 1 && Mat[i + 1, j] == 1))
for(int i = v2.Coordinates[0]; i < v3.Coordinates[0]; i++)
int j = v2.Coordinates[1];
|| (j != 0 && Mat[i, j - 1] == 1)
|| (j != M - 1 && Mat[i, j + 1] == 1))
for(int j = v3.Coordinates[1]; j > v0.Coordinates[1]; j--)
int i = v3.Coordinates[1];
|| (i != 0 && Mat[i - 1, j] == 1)
|| (i != M - 1 && Mat[i + 1, j] == 1))
return goodSquares.Count;
public void FindDiagonalVertices()
int[,] tempMatrix = new int[N + 2, M + 2];
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
tempMatrix[i + 1, j + 1] = Mat[i, j];
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
for(int k = 0; k < DiagonalTemplate.Templates.Count; k++)
int[,] template = DiagonalTemplate.Templates[k];
for(int p = 0; p < length; p++)
for(int q = 0; q < length; q++)
if(tempMatrix[i + 1 + p, j + 1 + q] != template[p, q])
Vertex vertex = new Vertex(new int[]{N, M}, false, k);
DiagonalVertices[k].Add(vertex);
public void FindVerticalVertices()
int[,] tempMatrix = new int[N + 2, M + 2];
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
tempMatrix[i + 1, j + 1] = Mat[i, j];
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
for(int k = 0; k < VerticalTemplate.Templates.Count; k++)
int[,] template = VerticalTemplate.Templates[k];
for(int p = 0; p < length; p++)
for(int q = 0; q < length; q++)
if(tempMatrix[i + 1 + p, j + 1 + q] != template[p, q])
Vertex vertex = new Vertex(new int[]{N, M}, true, k);
VerticalVertices[k].Add(vertex);
public Vertex[] Vertices{get;set;}
public Square(Vertex v0, Vertex v1, Vertex v2, Vertex v3)
Vertices = new Vertex[]{v0, v1, v2, v3};