#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define INF (1LL<<59)
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
        if(m%2)
            r=(r*n)%p;
        n=(n*n)%p;
        m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int segt[1005][9000];
int hor[1005][1005],ver[1005][1005];
void build(int n,int s,int e,int j)
{
	if(s==e)
	{
		segt[j][n]=hor[s][j];
		return ;
	}
	int m=(s+e)/2;
	build(2*n,s,m,j);
	build(2*n+1,m+1,e,j);
	segt[j][n]=min(segt[j][2*n],segt[j][2*n+1]);
}
int query(int n,int s,int e,int l,int r,int j)
{
	if(s>r||l>e)
		return inf;
	if(l<=s&&e<=r)
		return segt[j][n];
	int m=(s+e)/2;
	return min(query(2*n,s,m,l,min(m,r),j),query(2*n+1,m+1,e,max(l,m+1),r,j));
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n,m;
   ull ans=0;
   cin>>n>>m;
   char a[n][m];
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;j++)
   	{
   		cin>>a[i][j];
   	}
   }
   memset(hor,0,sizeof(hor));
   memset(ver,0,sizeof(ver));
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;)
   	{
   		int c=0;
   		char t=a[i][j];
   		int tj=j;
   		while(tj<m&&a[i][tj]==t)
   			tj++,c++;
   		while(j<tj)
   		{
   			hor[i][j]=c;
   			c--;
   			j++;
   		}
   	}
   }
   for(int j=0;j<m;j++)
   {
   	for(int i=0;i<n;)
   	{
   		int c=0;
   		char t=a[i][j];
   		int ti=i;
   		while(ti<n&&a[ti][j]==t)
   			ti++,c++;
   		while(i<ti)
   		{
   			ver[i][j]=c;
   			c--;
   			i++;
   		}
   	}
   }
   for(int i=0;i<m;i++)
   {
   	build(1,0,n-1,i);
   }
   // for(int i=0;i<n;i++)
   // {
   // 	for(int j=0;j<m;j++)
   // 	{
   // 		cout<<hor[i][j]<<' ';
   // 	}
   // 	cout<<endl;
   // }
   // for(int i=0;i<n;i++)
   // {
   // 	for(int j=0;j<m;j++)
   // 	{
   // 		cout<<ver[i][j]<<' ';
   // 	}
   // 	cout<<endl;
   // }
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;j++)
   	{
   		int x=i,y=j,Y=hor[i][j];
   		//set<char>t;
   		//t.insert(a[i][j]);
   		if(x+ver[i][j]<n)
   		{
   			x=x+ver[i][j];
   			//t.insert(a[x][y]);
   			Y=min(Y,hor[x][y]);
   			if(ver[i][j]==ver[x][y])
   			{
   				int tx=x;
   				x=x+ver[i][j];
   				//t.insert(a[x][y]);
   				if(x<n&&ver[x][j]>=ver[tx][j]){
   					//t.insert(a[x][y]);
   					Y=query(1,0,n-1,i,i+3*ver[i][j]-1,j);
   					//if(a[i][j]!=a[x][j])
   						ans=ans+Y;
   				}
   			}
   		}
   	}
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define INF (1LL<<59)
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
        if(m%2)
            r=(r*n)%p;
        n=(n*n)%p;
        m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int segt[1005][9000];
int hor[1005][1005],ver[1005][1005];
void build(int n,int s,int e,int j)
{
	if(s==e)
	{
		segt[j][n]=hor[s][j];
		return ;
	}
	int m=(s+e)/2;
	build(2*n,s,m,j);
	build(2*n+1,m+1,e,j);
	segt[j][n]=min(segt[j][2*n],segt[j][2*n+1]);
}
int query(int n,int s,int e,int l,int r,int j)
{
	if(s>r||l>e)
		return inf;
	if(l<=s&&e<=r)
		return segt[j][n];
	int m=(s+e)/2;
	return min(query(2*n,s,m,l,min(m,r),j),query(2*n+1,m+1,e,max(l,m+1),r,j));
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n,m;
   ull ans=0;
   cin>>n>>m;
   char a[n][m];
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;j++)
   	{
   		cin>>a[i][j];
   	}
   }
   memset(hor,0,sizeof(hor));
   memset(ver,0,sizeof(ver));
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;)
   	{
   		int c=0;
   		char t=a[i][j];
   		int tj=j;
   		while(tj<m&&a[i][tj]==t)
   			tj++,c++;
   		while(j<tj)
   		{
   			hor[i][j]=c;
   			c--;
   			j++;
   		}
   	}
   }
   for(int j=0;j<m;j++)
   {
   	for(int i=0;i<n;)
   	{
   		int c=0;
   		char t=a[i][j];
   		int ti=i;
   		while(ti<n&&a[ti][j]==t)
   			ti++,c++;
   		while(i<ti)
   		{
   			ver[i][j]=c;
   			c--;
   			i++;
   		}
   	}
   }
   for(int i=0;i<m;i++)
   {
   	build(1,0,n-1,i);
   }
   // for(int i=0;i<n;i++)
   // {
   // 	for(int j=0;j<m;j++)
   // 	{
   // 		cout<<hor[i][j]<<' ';
   // 	}
   // 	cout<<endl;
   // }
   // for(int i=0;i<n;i++)
   // {
   // 	for(int j=0;j<m;j++)
   // 	{
   // 		cout<<ver[i][j]<<' ';
   // 	}
   // 	cout<<endl;
   // }
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;j++)
   	{
   		int x=i,y=j,Y=hor[i][j];
   		//set<char>t;
   		//t.insert(a[i][j]);
   		if(x+ver[i][j]<n)
   		{
   			x=x+ver[i][j];
   			//t.insert(a[x][y]);
   			Y=min(Y,hor[x][y]);
   			if(ver[i][j]==ver[x][y])
   			{
   				int tx=x;
   				x=x+ver[i][j];
   				//t.insert(a[x][y]);
   				if(x<n&&ver[x][j]>=ver[tx][j]){
   					//t.insert(a[x][y]);
   					Y=query(1,0,n-1,i,x+ver[x][j]-1,j);
   					//if(a[i][j]!=a[x][j])
   						ans=ans+Y;
   				}
   			}
   		}
   	}
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define INF (1LL<<59)
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
        if(m%2)
            r=(r*n)%p;
        n=(n*n)%p;
        m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int segt[1005][6000];
int hor[1005][1005],ver[1005][1005];
void build(int n,int s,int e,int j)
{
	if(s==e)
	{
		segt[j][n]=hor[s][j];
		return ;
	}
	int m=(s+e)/2;
	build(2*n,s,m,j);
	build(2*n+1,m+1,e,j);
	segt[j][n]=min(segt[j][2*n],segt[j][2*n+1]);
}
int query(int n,int s,int e,int l,int r,int j)
{
	if(s>r||l>e)
		return inf;
	if(l<=s&&e<=r)
		return segt[j][n];
	int m=(s+e)/2;
	return min(query(2*n,s,m,l,r,j),query(2*n+1,m+1,e,l,r,j));
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n,m;
   ull ans=0;
   cin>>n>>m;
   char a[n][m];
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;j++)
   	{
   		cin>>a[i][j];
   	}
   }
   memset(hor,0,sizeof(hor));
   memset(ver,0,sizeof(ver));
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;)
   	{
   		int c=0;
   		char t=a[i][j];
   		int tj=j;
   		while(tj<m&&a[i][tj]==t)
   			tj++,c++;
   		while(j<tj)
   		{
   			hor[i][j]=c;
   			c--;
   			j++;
   		}
   	}
   }
   for(int j=0;j<m;j++)
   {
   	for(int i=0;i<n;)
   	{
   		int c=0;
   		char t=a[i][j];
   		int ti=i;
   		while(ti<n&&a[ti][j]==t)
   			ti++,c++;
   		while(i<ti)
   		{
   			ver[i][j]=c;
   			c--;
   			i++;
   		}
   	}
   }
   for(int i=0;i<m;i++)
   {
   	build(1,0,n-1,i);
   }
   // for(int i=0;i<n;i++)
   // {
   // 	for(int j=0;j<m;j++)
   // 	{
   // 		cout<<hor[i][j]<<' ';
   // 	}
   // 	cout<<endl;
   // }
   // for(int i=0;i<n;i++)
   // {
   // 	for(int j=0;j<m;j++)
   // 	{
   // 		cout<<ver[i][j]<<' ';
   // 	}
   // 	cout<<endl;
   // }
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;j++)
   	{
   		int x=i,y=j,Y=hor[i][j];
   		//set<char>t;
   		//t.insert(a[i][j]);
   		if(x+ver[i][j]<n)
   		{
   			x=x+ver[i][j];
   			//t.insert(a[x][y]);
   			Y=min(Y,hor[x][y]);
   			if(ver[i][j]==ver[x][y])
   			{
   				int tx=x;
   				x=x+ver[i][j];
   				//t.insert(a[x][y]);
   				if(x<n&&ver[x][j]>=ver[tx][j]){
   					//t.insert(a[x][y]);
   					Y=query(1,0,n-1,i,x+ver[x][j]-1,j);
   					//if(a[i][j]!=a[x][j])
   						ans=ans+Y;
   				}
   			}
   		}
   	}
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define INF (1LL<<59)
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
        if(m%2)
            r=(r*n)%p;
        n=(n*n)%p;
        m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
bool comp(string x,string y)
{
	if(x.size()!=y.size())
		return x.size()<y.size();
	return x<y;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n,m,ans=0;
   cin>>n>>m;
   char a[n][m];
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;j++)
   	{
   		cin>>a[i][j];
   	}
   }
   int hor[n][m],ver[n][m];
   memset(hor,0,sizeof(hor));
   memset(ver,0,sizeof(ver));
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;)
   	{
   		int c=0;
   		char t=a[i][j];
   		int tj=j;
   		while(tj<m&&a[i][tj]==t)
   			tj++,c++;
   		while(j<tj)
   		{
   			hor[i][j]=c;
   			c--;
   			j++;
   		}
   	}
   }
   for(int j=0;j<m;j++)
   {
   	for(int i=0;i<n;)
   	{
   		int c=0;
   		char t=a[i][j];
   		int ti=i;
   		while(ti<n&&a[ti][j]==t)
   			ti++,c++;
   		while(i<ti)
   		{
   			ver[i][j]=c;
   			c--;
   			i++;
   		}
   	}
   }
   // for(int i=0;i<n;i++)
   // {
   // 	for(int j=0;j<m;j++)
   // 	{
   // 		cout<<hor[i][j]<<' ';
   // 	}
   // 	cout<<endl;
   // }
   // for(int i=0;i<n;i++)
   // {
   // 	for(int j=0;j<m;j++)
   // 	{
   // 		cout<<ver[i][j]<<' ';
   // 	}
   // 	cout<<endl;
   // }
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<m;j++)
   	{
   		int x=i,y=j,Y=hor[i][j];
   		set<char>t;
   		t.insert(a[i][j]);
   		if(x+ver[x][y]<n)
   		{
   			x=x+ver[x][y];
   			t.insert(a[x][y]);
   			Y=min(Y,hor[x][y]);
   			if(ver[i][j]==ver[x][y])
   			{
   				x=x+ver[i][j];
   				//t.insert(a[x][y]);
   				if(x<n&&ver[x][j]>=ver[i][j]){
   					t.insert(a[x][y]);

   					Y=min(Y,hor[x][j]);
   					if(t.size()==3)
   						ans=ans+Y;
   				}
   			}
   		}
   	}
   }
   cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,m,i,j,k;
    cin>>n>>m;

    char c[n][m];

    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            cin>>c[i][j];

    long long int ans=0;

    vector<pair<long long int,string> > v[n];
    vector<pair<pair<long long int,long long int>,char> > temp;
    for(i=0;i<m;i++)
    {
        temp.resize(0);
        char temp_char=c[0][i];
        long long int xx=1;
        long long int temp_pos=0;
        for(j=1;j<n;j++)
        {
            if(temp_char==c[j][i])
            {
                xx++;
            }
            else
            {
                temp.push_back(make_pair(make_pair(xx,temp_pos),temp_char));
                temp_char=c[j][i];
                temp_pos=j;
                xx=1;
            }
        }
        temp.push_back(make_pair(make_pair(xx,temp_pos),temp_char));

        long long int si=temp.size();
        if(si<3)
            continue;

        for(j=1;j<si-1;j++)
        {
            if(temp[j].second!=temp[j-1].second&&temp[j].second!=temp[j+1].second&&temp[j].first.first>=temp[j-1].first.first&&temp[j].first.first>=temp[j+1].first.first)
            {
                string s="";
                for(k=0;k<temp[j].first.first;k++)
                    s+=temp[j-1].second;
                for(k=0;k<temp[j].first.first;k++)
                    s+=temp[j].second;
                for(k=0;k<temp[j].first.first;k++)
                    s+=temp[j+1].second;

                v[temp[j].first.second-temp[j].first.first].push_back({i,s});

            }
        }
    }

    for(i=0;i<n;i++)
    {
        long long int si=v[i].size();
        if(si<1)
            continue;

        string temp_str=v[i][0].second;
        long long int temp_idx=0;
        long long int xx=1;
        for(j=1;j<si;j++)
        {
            if(temp_str==v[i][j].second&&temp_idx+1==j)
            {
                xx++;
            }
            else
            {
                ans+=(xx*(xx+1))/2;
                temp_str=v[i][j].second;
                xx=1;

            }
            temp_idx=j;
        }
        ans+=(xx*(xx+1))/2;
    }

    cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long int n,m,i,j,k,l,p;
    cin>>n>>m;

    char c[n][m];

    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            cin>>c[i][j];

    long long int ans=0;

    for(i=0;i<n;i++)
    {
        for(j=i+2;j<n;j++)
        {

            long long int height=j-i+1;
            if(height%3!=0)
                continue;

            long long int x=height/3;

            for(k=0;k<m;k++)
            {
                char temp=c[i][k];
                char temp1=c[i+x][k];
                char temp2=c[i+x+x][k];
                if(temp!=temp1&&temp1!=temp2)
                {
                    long long int t=1;
                    for(l=i;l<i+x;l++)
                    {
                        if(c[l][k]!=c[i][k])
                        {
                            t=0;
                            break;
                        }
                    }
                    if(t==0)
                        continue;
                    for(l=i+x;l<i+x+x;l++)
                    {
                        if(c[l][k]!=c[i+x][k])
                        {
                            t=0;
                            break;
                        }
                    }
                    if(t==0)
                        continue;
                    for(l=i+x+x;l<i+x+x+x;l++)
                    {
                        if(c[l][k]!=c[i+x+x][k])
                        {
                            t=0;
                            break;
                        }
                    }
                    if(t==0)
                        continue;
                    long long int xx=1;
                    t=1;
                    for(l=k+1;l<m;l++)
                    {
                        for(p=i;p<=j;p++)
                        {
                            if(c[p][l]!=c[p][k])
                            {
                                t=0;
                                break;
                            }
                        }

                        if(t==0)
                        {
                            ans+=(xx*(xx+1))/2;
                            k=l-1;
                            break;
                        }
                        xx++;
                    }
                    if(t)
                    {
                        //cout<<xx<<"\n";

                        ans+=(xx*(xx+1))/2;
                        k=l-1;
                    }
                }
            }

        }
    }

    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long int n,m,i,j,k,l,p;
    cin>>n>>m;

    char c[n][m];

    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            cin>>c[i][j];

    long long int ans=0;

    for(i=0;i<n;i++)
    {
        for(j=i+2;j<n;j++)
        {

            long long int height=j-i+1;
            if(height%3!=0)
                continue;

            long long int x=height/3;

            for(k=0;k<m;k++)
            {
                char temp=c[i][k];
                char temp1=c[i+x][k];
                char temp2=c[i+x+x][k];
                if(temp!=temp1&&temp1!=temp2)
                {
                    long long int t=1;
                    for(l=i;l<i+x;l++)
                    {
                        if(c[l][k]!=c[i][k])
                        {
                            t=0;
                            break;
                        }
                    }
                    if(t==0)
                        continue;
                    for(l=i+x;l<i+x+x;l++)
                    {
                        if(c[l][k]!=c[i+x][k])
                        {
                            t=0;
                            break;
                        }
                    }
                    if(t==0)
                        continue;
                    for(l=i+x+x;l<i+x+x+x;l++)
                    {
                        if(c[l][k]!=c[i+x+x][k])
                        {
                            t=0;
                            break;
                        }
                    }
                    if(t==0)
                        continue;
                    long long int xx=1;
                    t=1;
                    for(l=k+1;l<m;l++)
                    {
                        for(p=i;p<=j;p++)
                        {
                            if(c[p][l]!=c[p][k])
                            {
                                t=0;
                                break;
                            }
                        }

                        if(t==0)
                        {
                            ans+=(xx*(xx+1))/2;
                            k=l-1;
                            break;
                        }
                        xx++;
                    }
                    if(t)
                    {
                        //cout<<xx<<"\n";

                        ans+=(xx*(xx+1))/2;
                        k=l-1;
                    }
                }
            }

        }
    }

    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,m,i,j,k;
    cin>>n>>m;

    char c[n][m];

    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            cin>>c[i][j];

    long long int ans=0;

    vector<pair<long long int,string> > v[n];
    vector<pair<pair<long long int,long long int>,char> > temp;
    for(i=0;i<m;i++)
    {
        temp.resize(0);
        char temp_char=c[0][i];
        long long int xx=1;
        long long int temp_pos=0;
        for(j=1;j<n;j++)
        {
            if(temp_char==c[j][i])
            {
                xx++;
            }
            else
            {
                temp.push_back(make_pair(make_pair(xx,temp_pos),temp_char));
                temp_char=c[j][i];
                temp_pos=j;
                xx=1;
            }
        }
        temp.push_back(make_pair(make_pair(xx,temp_pos),temp_char));

        long long int si=temp.size();
        if(si<3)
            continue;

        for(j=1;j<si-1;j++)
        {
            if(temp[j].second!=temp[j-1].second&&temp[j].second!=temp[j+1].second&&temp[j].first.first<=temp[j-1].first.first&&temp[j].first.first<=temp[j+1].first.first)
            {
                string s="";
                for(k=0;k<temp[j].first.first;k++)
                    s+=temp[j-1].second;
                for(k=0;k<temp[j].first.first;k++)
                    s+=temp[j].second;
                for(k=0;k<temp[j].first.first;k++)
                    s+=temp[j+1].second;
                //cout<<temp[j].first.second<<" "<<temp[j].first.first<<"\n";
                v[temp[j].first.second-temp[j].first.first].push_back({i,s});

            }
        }
    }

    for(i=0;i<n;i++)
    {
        long long int si=v[i].size();
        if(si<1)
            continue;

        string temp_str=v[i][0].second;
        long long int temp_idx=v[i][0].first;
        long long int xx=1;
        for(j=1;j<si;j++)
        {
            if(temp_str==v[i][j].second&&temp_idx+1==v[i][j].first)
            {
                xx++;
            }
            else
            {
                ans+=(xx*(xx+1))/2;
                temp_str=v[i][j].second;
                xx=1;

            }
            temp_idx=v[i][j].first;
        }
        ans+=(xx*(xx+1))/2;
    }

    cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long int n,m,i,j,k,l,p;
    cin>>n>>m;

    char c[n][m];

    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            cin>>c[i][j];

    long long int ans=0;

    for(i=0;i<n;i++)
    {
        for(j=i+2;j<n;j++)
        {

            long long int height=j-i+1;
            if(height%3!=0)
                continue;

            long long int x=height/3;

            for(k=0;k<m;k++)
            {
                char temp=c[i][k];
                char temp1=c[i+x][k];
                char temp2=c[i+x+x][k];
                if(temp!=temp1&&temp1!=temp2)
                {
                    long long int t=1;
                    for(l=i;l<i+x;l++)
                    {
                        if(c[l][k]!=c[i][k])
                        {
                            t=0;
                            break;
                        }
                    }
                    if(t==0)
                        continue;
                    for(l=i+x;l<i+x+x;l++)
                    {
                        if(c[l][k]!=c[i+x][k])
                        {
                            t=0;
                            break;
                        }
                    }
                    if(t==0)
                        continue;
                    for(l=i+x+x;l<i+x+x+x;l++)
                    {
                        if(c[l][k]!=c[i+x+x][k])
                        {
                            t=0;
                            break;
                        }
                    }
                    if(t==0)
                        continue;
                    long long int xx=1;
                    t=1;
                    for(l=k+1;l<m;l++)
                    {
                        for(p=i;p<=j;p++)
                        {
                            if(c[p][l]!=c[p][k])
                            {
                                t=0;
                                break;
                            }
                        }

                        if(t==0)
                        {
                            ans+=(xx*(xx+1))/2;
                            k=l-1;
                            break;
                        }
                        xx++;
                    }
                    if(t)
                    {
                        ans+=(xx*(xx+1))/2;
                    }
                }
            }

        }
    }

    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,m,i,j,k;
    cin>>n>>m;

    char c[n][m];

    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            cin>>c[i][j];

    long long int ans=0;

    vector<pair<long long int,string> > v[n];
    vector<pair<pair<long long int,long long int>,char> > temp;
    for(i=0;i<m;i++)
    {
        temp.resize(0);
        char temp_char=c[0][i];
        long long int xx=1;
        long long int temp_pos=0;
        for(j=1;j<n;j++)
        {
            if(temp_char==c[j][i])
            {
                xx++;
            }
            else
            {
                temp.push_back(make_pair(make_pair(xx,temp_pos),temp_char));
                temp_char=c[j][i];
                temp_pos=j;
                xx=1;
            }
        }
        temp.push_back(make_pair(make_pair(xx,temp_pos),temp_char));

        long long int si=temp.size();
        if(si<3)
            continue;

        for(j=1;j<si-1;j++)
        {
            if(temp[j].second!=temp[j-1].second&&temp[j].second!=temp[j+1].second&&temp[j].first.first<=temp[j-1].first.first&&temp[j].first.first<=temp[j+1].first.first)
            {
                string s="";
                for(k=0;k<temp[j].first.first;k++)
                    s+=temp[j-1].second;
                for(k=0;k<temp[j].first.first;k++)
                    s+=temp[j].second;
                for(k=0;k<temp[j].first.first;k++)
                    s+=temp[j+1].second;
                //cout<<temp[j].first.second<<" "<<temp[j].first.first<<"\n";
                v[temp[j].first.second-temp[j].first.first].push_back({i,s});

            }
        }
    }

    for(i=0;i<n;i++)
    {
        long long int si=v[i].size();
        if(si<1)
            continue;

        string temp_str=v[i][0].second;
        long long int temp_idx=0;
        long long int xx=1;
        for(j=1;j<si;j++)
        {
            if(temp_str==v[i][j].second&&temp_idx+1==j)
            {
                xx++;
            }
            else
            {
                ans+=(xx*(xx+1))/2;
                temp_str=v[i][j].second;
                xx=1;

            }
            temp_idx=j;
        }
        ans+=(xx*(xx+1))/2;
    }

    cout<<ans;

}


#include <cstdio>
#include <iostream>

using namespace std;

const int N = 1000;
const int M = 1000;

int n, m;
char a[N + 5][M + 5];

int h[N + 5][M + 5];

bool equal_prev(int i, int j)
{
    if (i - 2 * h[i][j] < 0 || j == 0)
        return false;

    for (int t = 0; t <= 2; t++)
    {
        int cur_i = i - t * h[i][j];
        if (a[cur_i][j - 1] != a[cur_i][j])
            return false;
    }

    int second = i - h[i][j];
    if (h[i][j - 1] != h[i][j] || h[second][j - 1] != h[second][j])
        return false;

    return true;
}

bool equal_layer(int i, int j)
{
    if (i - 2 * h[i][j] < 0)
        return false;

    int second = i - h[i][j];
    if (h[second][j] != h[i][j])
        return false;

    int third = i - 2 * h[i][j];
    if (h[third][j] < h[i][j])
        return false;

    return true;
}

int main()
{
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    }

    for (int j = 0; j < m; j++)
        h[0][j] = 1;

    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        int cur_cnt = 0;

        for (int j = 0; j < m; j++)
        {
            if (a[i][j] == a[i - 1][j])
                h[i][j] = h[i - 1][j] + 1;
            else
                h[i][j] = 1;

            if (equal_layer(i, j))
            {
                if (equal_prev(i, j))
                {
                    cur_cnt += 1;
                }
                else
                {
                    cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                    cur_cnt = 1;
                }
            }
            else
            {
                cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                cur_cnt = 0;
            }
        }

        if (equal_layer(i, m - 1))
            cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
    }

    printf("%d", cnt);

    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

const int N = 1000;
const int M = 1000;

int n, m;
char a[N + 5][M + 5];

int h[N + 5][M + 5];

bool equal_prev(int i, int j)
{
    if (i - 2 * h[i][j] < 0)
        return false;

    for (int t = 0; t <= 2; t++)
    {
        int cur_i = i - t * h[i][j];
        if (h[cur_i][j] != h[i][j] || a[cur_i][j - 1] != a[cur_i][j])
            return false;
    }

    return true;
}

bool equal_layer(int i, int j)
{
    if (i - 2 * h[i][j] < 0)
        return false;

    for (int t = 1; t <= 2; t++)
    {
        int cur_i = i - t * h[i][j];
        if (h[cur_i][j] != h[i][j])
            return false;
    }

    return true;
}

int main()
{
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    }

    for (int j = 0; j < m; j++)
        h[0][j] = 1;

    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        int cur_cnt = 0;

        for (int j = 0; j < m; j++)
        {
            if (a[i][j] == a[i - 1][j])
                h[i][j] = h[i - 1][j] + 1;
            else
                h[i][j] = 1;

            if (equal_layer(i, j))
            {
                if (equal_prev(i, j))
                {
                    cur_cnt += 1;
                }
                else
                {
                    cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                    cur_cnt = 1;
                }
            }
            else
            {
                cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                cur_cnt = 0;
            }
        }

        if (equal_layer(i, m - 1))
            cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
    }

    printf("%d", cnt);

    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

const int N = 1000;
const int M = 1000;

int n, m;
char a[N + 5][M + 5];

int h[N + 5][M + 5];

bool check(int i, int j)
{
    if (i - 2 * h[i][j] < 0)
        return false;

    int firstLayer = h[i][j];
    int secondLayer = h[i - h[i][j]][j];
    int thirdLayer = h[i - 2 * h[i][j]][j];

    if (firstLayer == secondLayer && firstLayer == thirdLayer)
        return true;
    else
        return false;
}

int main()
{
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    }

    for (int j = 0; j < m; j++)
        h[0][j] = 1;

    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        int cur_cnt = 0;

        for (int j = 0; j < m; j++)
        {
            if (a[i][j] == a[i - 1][j])
                h[i][j] = h[i - 1][j] + 1;
            else
                h[i][j] = 1;

            if (check(i, j))
                cur_cnt += 1;
            else
            {
                cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                cur_cnt = 0;
            }
        }

        if (i - 2 * h[i][m - 1] >= 0)
            cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
    }

    printf("%d", cnt);

    return 0;
}


#include <cstdio>

using namespace std;

const int N = 1000;
const int M = 1000;

int n, m;
char a[N + 5][M + 5];

int h[N + 5][M + 5];

bool check(int i, int j)
{
    if (i - 2 * h[i][j] < 0)
        return false;

    int firstLayer = h[i][j];
    int secondLayer = h[i - h[i][j]][j];
    int thirdLayer = h[i - 2 * h[i][j]][j];

    if (firstLayer == secondLayer && firstLayer == thirdLayer)
        return true;
    else
        return false;
}

int main()
{
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            scanf("%c", &a[i][j]);
    }

    for (int j = 0; j < m; j++)
        a[0][j] = 1;

    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        int cur_cnt = 0;

        for (int j = 0; j < m; j++)
        {
            if (a[i][j] == a[i - 1][j])
                h[i][j] = h[i - 1][j] + 1;
            else
                h[i][j] = 0;

            if (check(i, j))
                cur_cnt += 1;
            else
            {
                cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                cur_cnt = 0;
            }
        }

        cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
    }

    printf("%d", cnt);

    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

const int N = 1000;
const int M = 1000;

int n, m;
char a[N + 5][M + 5];

int h[N + 5][M + 5];

bool equal_prev(int i, int j)
{
    if (i - 2 * h[i][j] < 0 || j == 0)
        return false;

    for (int t = 0; t <= 2; t++)
    {
        int cur_i = i - t * h[i][j];
        if (a[cur_i][j - 1] != a[cur_i][j])
            return false;
    }

    int second = i - h[i][j];
    int third = i - 2 * h[i][j];
    if (h[i][j - 1] != h[i][j] || h[second][j - 1] != h[second][j] || h[third][j - 1] < h[third][j])
        return false;

    return true;
}

bool equal_layer(int i, int j)
{
    if (i - 2 * h[i][j] < 0)
        return false;

    int second = i - h[i][j];
    if (h[second][j] != h[i][j])
        return false;

    int third = i - 2 * h[i][j];
    if (h[third][j] < h[i][j])
        return false;

    return true;
}

int main()
{
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    }

    for (int j = 0; j < m; j++)
        h[0][j] = 1;

    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        int cur_cnt = 0;

        for (int j = 0; j < m; j++)
        {
            if (a[i][j] == a[i - 1][j])
                h[i][j] = h[i - 1][j] + 1;
            else
                h[i][j] = 1;

            if (equal_layer(i, j))
            {
                if (equal_prev(i, j))
                {
                    cur_cnt += 1;
                }
                else
                {
                    cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                    cur_cnt = 1;
                }
            }
            else
            {
                cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                cur_cnt = 0;
            }
        }

        if (equal_layer(i, m - 1))
            cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
    }

    printf("%d", cnt);

    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

const int N = 1000;
const int M = 1000;

int n, m;
char a[N + 5][M + 5];

int h[N + 5][M + 5];

bool equal_prev(int i, int j)
{
    if (i - 2 * h[i][j] < 0)
        return false;

    for (int t = 0; t <= 2; t++)
    {
        int cur_i = i - t * h[i][j];
        if (a[cur_i][j - 1] != a[cur_i][j])
            return false;
    }

    return true;
}

bool equal_layer(int i, int j)
{
    if (i - 2 * h[i][j] < 0)
        return false;

    int second = i - h[i][j];
    if (h[second][j] != h[i][j])
        return false;

    int third = i - 2 * h[i][j];
    if (h[third][j] < h[i][j])
        return false;

    return true;
}

int main()
{
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    }

    for (int j = 0; j < m; j++)
        h[0][j] = 1;

    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        int cur_cnt = 0;

        for (int j = 0; j < m; j++)
        {
            if (a[i][j] == a[i - 1][j])
                h[i][j] = h[i - 1][j] + 1;
            else
                h[i][j] = 1;

            if (equal_layer(i, j))
            {
                if (equal_prev(i, j))
                {
                    cur_cnt += 1;
                }
                else
                {
                    cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                    cur_cnt = 1;
                }
            }
            else
            {
                cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                cur_cnt = 0;
            }
        }

        if (equal_layer(i, m - 1))
            cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
    }

    printf("%d", cnt);

    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

const int N = 1000;
const int M = 1000;

int n, m;
char a[N + 5][M + 5];

int h[N + 5][M + 5];

bool equal_prev(int i, int j)
{
    if (i - 2 * h[i][j] < 0 || j == 0)
        return false;

    for (int t = 0; t <= 2; t++)
    {
        int cur_i = i - t * h[i][j];
        if (a[cur_i][j - 1] != a[cur_i][j])
            return false;
    }

    return true;
}

bool equal_layer(int i, int j)
{
    if (i - 2 * h[i][j] < 0)
        return false;

    int second = i - h[i][j];
    if (h[second][j] != h[i][j])
        return false;

    int third = i - 2 * h[i][j];
    if (h[third][j] < h[i][j])
        return false;

    return true;
}

int main()
{
    scanf("%d %d", &n, &m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    }

    for (int j = 0; j < m; j++)
        h[0][j] = 1;

    int cnt = 0;
    for (int i = 1; i < n; i++)
    {
        int cur_cnt = 0;

        for (int j = 0; j < m; j++)
        {
            if (a[i][j] == a[i - 1][j])
                h[i][j] = h[i - 1][j] + 1;
            else
                h[i][j] = 1;

            if (equal_layer(i, j))
            {
                if (equal_prev(i, j))
                {
                    cur_cnt += 1;
                }
                else
                {
                    cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                    cur_cnt = 1;
                }
            }
            else
            {
                cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
                cur_cnt = 0;
            }
        }

        if (equal_layer(i, m - 1))
            cnt += (cur_cnt - 1) * cur_cnt / 2 + cur_cnt;
    }

    printf("%d", cnt);

    return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	pair<char,pair<char,char>>data[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	char db[3];
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		db[current] = currentChar;
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				currentChar = input[i][j];
				db[current] = currentChar;
			}
			int firstIndex = (current+1)%3;
			int secondIndex = (current+2)%3;
			int thirdIndex = current;
			int third = ctr[current];
			int first = ctr[(current+1)%3];
			int second = ctr[(current+2)%3];
			//cout << first << second << third<<endl;
			if(first>=second && second == third && second > 0){
				int prevCol = 0;
				//cout << " i = " << i  << " j = " << j << "matrix[i][j-1].first = " <<matrix[i][j-1].second << "uy = "<< ctr[0]<<endl;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[0]){
					//cout << " i = " << i << " j = " << j << " db[firstIndex] = " << db[firstIndex] << " db[secondIndex] = " << db[secondIndex] << " db[thirdIndex] = " << db[thirdIndex] << endl;
					//cout << " i = " << i << " j = " << j << " data[i][j-1].first = " << data[i][j-1].first << " data[i][j-1].second.first = " << data[i][j-1].second.first << " data[i][j-1].second.second = " << data[i][j-1].second.second << endl;
					if(data[i][j-1].first == db[firstIndex] && data[i][j-1].second.first == db[secondIndex] && data[i][j-1].second.second == db[thirdIndex])
						prevCol = matrix[i][j-1].first;
				}
				matrix[i][j] = make_pair(prevCol+1,ctr[0]);
				data[i][j] = make_pair(db[firstIndex],make_pair(db[secondIndex],db[thirdIndex]));
				ans+=prevCol+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		db[1] = '1';
		db[2] = '2';
		current=0;
	}
	/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << " " ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	pair<char,pair<char,char>>data[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	char db[3];
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		db[current] = currentChar;
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				db[current] = currentChar;
				currentChar = input[i][j];
			}
			int third = ctr[current];
			int first = ctr[(current+1)%3];
			int second = ctr[(current+2)%3];
			//cout << first << second << third<<endl;
			if(first>=second && second == third && second > 0){
				int prevCol = 0;
				//cout << " i = " << i  << " j = " << j << "matrix[i][j-1].first = " <<matrix[i][j-1].second << "uy = "<< ctr[0]<<endl;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[0]){
					if(data[i][j-1].first == db[0] && data[i][j-1].second.first == db[1] && data[i][j-1].second.second == db[2])
						prevCol = matrix[i][j-1].first;
				}
				matrix[i][j] = make_pair(prevCol+1,ctr[0]);
				data[i][j] = make_pair(db[0],make_pair(db[1],db[2]));
				ans+=prevCol+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		db[1] = '1';
		db[2] = '2';
		current=0;
	}
	/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << " " ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	pair<char,pair<char,char>>data[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	char db[3];
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		db[current] = currentChar;
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				currentChar = input[i][j];
				db[current] = currentChar;
			}
			int firstIndex = (current+1)%3;
			int secondIndex = (current+2)%3;
			int thirdIndex = current;
			int third = ctr[current];
			int first = ctr[(current+1)%3];
			int second = ctr[(current+2)%3];
			//cout << first << second << third<<endl;
			if(first>=second && second == third && second > 0){
				int prevCol = 0;
				//cout << " i = " << i  << " j = " << j << "matrix[i][j-1].first = " <<matrix[i][j-1].first << " matrix[i][j-1].second = " << matrix[i][j-1].second << "uy = "<< ctr[0]<<endl;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[0]){
					//cout << " i = " << i << " j = " << j << " db[firstIndex] = " << db[firstIndex] << " db[secondIndex] = " << db[secondIndex] << " db[thirdIndex] = " << db[thirdIndex] << endl;
					//cout << " i = " << i << " j = " << j << " data[i][j-1].first = " << data[i][j-1].first << " data[i][j-1].second.first = " << data[i][j-1].second.first << " data[i][j-1].second.second = " << data[i][j-1].second.second << endl;
					if(data[i][j-1].first == db[firstIndex] && data[i][j-1].second.first == db[secondIndex] && data[i][j-1].second.second == db[thirdIndex])
						prevCol = matrix[i][j-1].first;
				}
				matrix[i][j] = make_pair(prevCol+1,ctr[second]);
				data[i][j] = make_pair(db[firstIndex],make_pair(db[secondIndex],db[thirdIndex]));
				ans+=prevCol+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		db[1] = '1';
		db[2] = '2';
		current=0;
	}
	/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << "" ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	pair<char,pair<char,char>>data[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	char db[3];
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		db[current] = currentChar;
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				currentChar = input[i][j];
				db[current] = currentChar;
			}
			int firstIndex = (current+1)%3;
			int secondIndex = (current+2)%3;
			int thirdIndex = current;
			int third = ctr[current];
			int first = ctr[(current+1)%3];
			int second = ctr[(current+2)%3];
			//cout << first << second << third<<endl;
			if(first>=second && second == third && second > 0){
				int prevCol = 0;
				//cout << " i = " << i  << " j = " << j << "matrix[i][j-1].first = " <<matrix[i][j-1].first << " matrix[i][j-1].second = " << matrix[i][j-1].second << " currentFlagL = "<< ctr[second]<<endl;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[second]){
					//cout << " i = " << i << " j = " << j << " db[firstIndex] = " << db[firstIndex] << " db[secondIndex] = " << db[secondIndex] << " db[thirdIndex] = " << db[thirdIndex] << endl;
					//cout << " i = " << i << " j = " << j << " data[i][j-1].first = " << data[i][j-1].first << " data[i][j-1].second.first = " << data[i][j-1].second.first << " data[i][j-1].second.second = " << data[i][j-1].second.second << endl;
					if(data[i][j-1].first == db[firstIndex] && data[i][j-1].second.first == db[secondIndex] && data[i][j-1].second.second == db[thirdIndex])
						prevCol = matrix[i][j-1].first;
				}
				matrix[i][j] = make_pair(prevCol+1,ctr[second]);
				data[i][j] = make_pair(db[firstIndex],make_pair(db[secondIndex],db[thirdIndex]));
				ans+=prevCol+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		db[1] = '1';
		db[2] = '2';
		current=0;
	}
	/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << "" ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				currentChar = input[i][j];
			}
			if(ctr[0]==ctr[1] && ctr[1] == ctr[2]){
				int prevCol = 0;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[0])
					prevCol = matrix[i][j-1].first;

				matrix[i][j] = make_pair(matrix[i][j-1].first+1,ctr[0]);
				ans+=matrix[i][j-1].first+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		current=0;
	}
/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << " " ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				currentChar = input[i][j];
			}
			if(ctr[0]==ctr[1] && ctr[1] == ctr[2]){
				int prevCol = 0;
				//cout << " i = " << i  << " j = " << j << "matrix[i][j-1].first = " <<matrix[i][j-1].second << "uy = "<< ctr[0]<<endl;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[0])
					prevCol = matrix[i][j-1].first;

				matrix[i][j] = make_pair(prevCol+1,ctr[0]);
				ans+=prevCol+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		current=0;
	}
/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << " " ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	pair<char,pair<char,char>>data[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	char db[3];
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		db[current] = currentChar;
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				currentChar = input[i][j];
				db[current] = currentChar;
			}
			int third = ctr[current];
			int first = ctr[(current+1)%3];
			int second = ctr[(current+2)%3];
			//cout << first << second << third<<endl;
			if(first>=second && second == third && second > 0){
				int prevCol = 0;
				//cout << " i = " << i  << " j = " << j << "matrix[i][j-1].first = " <<matrix[i][j-1].second << "uy = "<< ctr[0]<<endl;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[0]){
					//cout << " i = " << i << " j = " << j << " db[0] = " << db[0] << " db[1] = " << db[1] << " db[2] = " << db[2] << endl;
					if(data[i][j-1].first == db[0] && data[i][j-1].second.first == db[1] && data[i][j-1].second.second == db[2])
						prevCol = matrix[i][j-1].first;
				}
				matrix[i][j] = make_pair(prevCol+1,ctr[0]);
				data[i][j] = make_pair(db[0],make_pair(db[1],db[2]));
				ans+=prevCol+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		db[1] = '1';
		db[2] = '2';
		current=0;
	}
	/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << " " ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	pair<char,pair<char,char>>data[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	char db[3];
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		db[current] = currentChar;
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				db[current] = currentChar;
				currentChar = input[i][j];
			}
			if(ctr[0]>=ctr[1] && ctr[1] == ctr[2]){
				int prevCol = 0;
				//cout << " i = " << i  << " j = " << j << "matrix[i][j-1].first = " <<matrix[i][j-1].second << "uy = "<< ctr[0]<<endl;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[0]){
					if(data[i][j-1].first == db[0] && data[i][j-1].second.first == db[1] && data[i][j-1].second.second == db[2])
						prevCol = matrix[i][j-1].first;
				}
				matrix[i][j] = make_pair(prevCol+1,ctr[0]);
				data[i][j] = make_pair(db[0],make_pair(db[1],db[2]));
				ans+=prevCol+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		db[1] = '1';
		db[2] = '2';
		current=0;
	}
/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << " " ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	pair<char,pair<char,char>>data[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	char db[3];
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		db[current] = currentChar;
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				db[current] = currentChar;
				currentChar = input[i][j];
			}
			if(ctr[0]==ctr[1] && ctr[1] == ctr[2]){
				int prevCol = 0;
				//cout << " i = " << i  << " j = " << j << "matrix[i][j-1].first = " <<matrix[i][j-1].second << "uy = "<< ctr[0]<<endl;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[0]){
					if(data[i][j-1].first == db[0] && data[i][j-1].second.first == db[1] && data[i][j-1].second.second == db[2])
						prevCol = matrix[i][j-1].first;
				}
				matrix[i][j] = make_pair(prevCol+1,ctr[0]);
				data[i][j] = make_pair(db[0],make_pair(db[1],db[2]));
				ans+=prevCol+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		db[1] = '1';
		db[2] = '2';
		current=0;
	}
/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << " " ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
map <int,int>m;

int main() {
	// your code goes here

	register int i,j;
	long long int ans = 0;
	int n,m;
	cin >> n >> m;
	char input[n][m];
	pair<int,int> matrix[n][m];
	pair<char,pair<char,char>>data[n][m];
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> input[i][j];
		}
	}

	int ctr[3];
	ctr[0]=1;
	ctr[1]=0;
	ctr[2]=0;
	char db[3];
	int current = 0;

	for(j=0;j<m;j++){
		char currentChar = input[0][j];
		db[current] = currentChar;
		for(i=1;i<n;i++){
			if(input[i][j]==currentChar){
				ctr[current]++;
			}
			else{
				current=(current+1)%3;
				ctr[current]=1;
				currentChar = input[i][j];
				db[current] = currentChar;
			}
			int firstIndex = (current+1)%3;
			int secondIndex = (current+2)%3;
			int thirdIndex = current;
			int third = ctr[current];
			int first = ctr[(current+1)%3];
			int second = ctr[(current+2)%3];
			//cout << first << second << third<<endl;
			if(first>=second && second == third && second > 0){
				int prevCol = 0;
				//cout << " i = " << i  << " j = " << j << "matrix[i][j-1].first = " <<matrix[i][j-1].first << " matrix[i][j-1].second = " << matrix[i][j-1].second << " currentFlagL = "<< ctr[second]<<endl;
				if(j>0 && matrix[i][j-1].first>0 && matrix[i][j-1].second == ctr[secondIndex]){
					//cout << " i = " << i << " j = " << j << " db[firstIndex] = " << db[firstIndex] << " db[secondIndex] = " << db[secondIndex] << " db[thirdIndex] = " << db[thirdIndex] << endl;
					//cout << " i = " << i << " j = " << j << " data[i][j-1].first = " << data[i][j-1].first << " data[i][j-1].second.first = " << data[i][j-1].second.first << " data[i][j-1].second.second = " << data[i][j-1].second.second << endl;
					if(data[i][j-1].first == db[firstIndex] && data[i][j-1].second.first == db[secondIndex] && data[i][j-1].second.second == db[thirdIndex])
						prevCol = matrix[i][j-1].first;
				}
				matrix[i][j] = make_pair(prevCol+1,ctr[secondIndex]);
				data[i][j] = make_pair(db[firstIndex],make_pair(db[secondIndex],db[thirdIndex]));
				ans+=prevCol+1;
			}
			else{
				matrix[i][j] = make_pair(0,0);
			}
		}
		ctr[0]=1;
		ctr[1]=0;
		ctr[2]=0;
		db[1] = '1';
		db[2] = '2';
		current=0;
	}
	/*
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cout << matrix[i][j].first << "" ;
		}
		cout << endl;
	}*/
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

#define ForA(i,b,a) for(int i = a; i >= b; i--)
#define ForB(i,a,b) for(int i = a; i <= b; i++)
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mmset(arr, n) memset(arr, n, sizeof arr)
#define PI 3.1415926535897932384626433832795l
#define deci(n) cout<<fixed<<setprecision(n);
#define F first
#define S second
#define mk make_pair
#define pb push_back
#define ALPHA 27
#define IT(a , b) map <a , b> :: iterator
#define ll long long int
#define ld long double
#define debug(x) cerr << '#' << ' ' << x << '\n'
#define len(s) s.length()
#define maxn 1002

using namespace std;

const ll INF = (ll)(8e18);
const ll MOD = 1e9 + 7;

class Print{
   public:
   void print(int n){
       cout << n << '\n';
       exit(0);
   }
   void print(ll n){
       cout << n << '\n';
       exit(0);
   }
   void print(string s){
       cout << s << '\n';
       exit(0);
   }
   void print(ld n){
       cout << n << '\n';
       exit(0);
   }
};

char a[maxn][maxn];
int Right[maxn][maxn], bottom[maxn][maxn];

int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif
    Print _p;
    int n,m; cin >> n >> m;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++) cin >> a[i][j];
    }
    for(int i = 0; i < n; i++){
        for(int j = m - 1; j >= 0; j--){
            if(j == m - 1) Right[i][j] = 1;
            else Right[i][j] += (a[i][j] == a[i][j + 1] ? 1 + Right[i][j + 1] : 1);
        }
    }
    for(int i = 0; i < m; i++){
        for(int j = n - 1; j >= 0; j--){
            if(j == n - 1) bottom[j][i] = 1;
            else bottom[j][i] += (a[j][i] == a[j + 1][i] ? 1 + bottom[j + 1][i] : 1);
        }
    }
    ll ans = 0;
    for(int j = 0; j < m; j++){
        for(int i = 0; i < n; i++){
            int h = bottom[i][j];
            int r = Right[i][j];
            if((i + h) >= n || bottom[i + h][j] != h) continue;
            r = min(r, Right[i + h][j]);
            if((i + h + h) >= n || bottom[i + h + h][j] < h) continue;
            r = min(r, Right[i + h + h][j]);
            ans += 1LL * r;
        }
    }
    cout << ans << '\n';
    #ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#include <bits/stdc++.h>

#define ForA(i,b,a) for(int i = a; i >= b; i--)
#define ForB(i,a,b) for(int i = a; i <= b; i++)
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mmset(arr, n) memset(arr, n, sizeof arr)
#define PI 3.1415926535897932384626433832795l
#define deci(n) cout<<fixed<<setprecision(n);
#define F first
#define S second
#define mk make_pair
#define pb push_back
#define ALPHA 27
#define IT(a , b) map <a , b> :: iterator
#define ll long long int
#define ld long double
#define debug(x) cerr << '#' << ' ' << x << '\n'
#define len(s) s.length()
#define maxn 1002

using namespace std;

const ll INF = (ll)(8e18);
const ll MOD = 1e9 + 7;

class Print{
   public:
   void print(int n){
       cout << n << '\n';
       exit(0);
   }
   void print(ll n){
       cout << n << '\n';
       exit(0);
   }
   void print(string s){
       cout << s << '\n';
       exit(0);
   }
   void print(ld n){
       cout << n << '\n';
       exit(0);
   }
};

char a[maxn][maxn];
int Right[maxn][maxn], bottom[maxn][maxn];

int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif
    Print _p;
    int n,m; cin >> n >> m;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++) cin >> a[i][j];
    }
    for(int i = 0; i < n; i++){
        for(int j = m - 1; j >= 0; j--){
            if(j == m - 1) Right[i][j] = 1;
            else Right[i][j] += (a[i][j] == a[i][j + 1] ? 1 + Right[i][j + 1] : 1);
        }
    }
    for(int i = 0; i < m; i++){
        for(int j = n - 1; j >= 0; j--){
            if(j == n - 1) bottom[j][i] = 1;
            else bottom[j][i] += (a[j][i] == a[j + 1][i] ? 1 + bottom[j + 1][i] : 1);
        }
    }
    ll ans = 0;
    for(int j = 0; j < m; j++){
        for(int i = 0; i < n; i++){
            int h = bottom[i][j];
            int r = Right[i][j];
            for(int k = i; k < i + h; k++) r = min(r, Right[k][j]);
            if((i + h) >= n || bottom[i + h][j] != h) continue;
            for(int k = i + h; k < i + h + h; k++) r = min(r, Right[k][j]);
            if((i + h + h) >= n || bottom[i + h + h][j] < h) continue;
            for(int k = i + h + h; k < i + h + h + h; k++) r = min(r, Right[k][j]);
            ans += 1LL * r;
        }
    }
    cout << ans << '\n';
    #ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}


#include <iostream>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	int t, n, sum;
	cin >> t;
	for(int i = 0; i < t; i++) {
		cin >> n;
		int a[n];
		bool Possible_Sums[8001] = {0};
		for(int j = 0; j < n; j++) {
			cin >> a[j];
		}
		for(int k = 0; k < n; k++) {
			sum = a[k];
			for(int j = k + 1; j < n; j++) {
				sum += a[j];
				if(sum > n) break;
				Possible_Sums[sum] = true;
			}
		}
		int count = 0;
		for(int j = 0; j < n; j++) {
			if(Possible_Sums[a[j]]){
				count++;
//				cout << a[j] << endl;
			}
		}
		cout << count << endl;
	}
	return 0;
}


#include <iostream>
using namespace std;
int first, second;
int sum[33000];

void build(int l, int r, int a[], int v) {
	if(l == r) {
		sum[v] = a[l];
	}
	else{
		int mid = (l + r) / 2;
		build(l,mid, a,  2 * v);
		build(mid + 1, r, a, 2 * v + 1);
		sum[v] = sum[2 * v] + sum[2 * v + 1];
	}
}
int Summa (int v, int tl, int tr, int l, int r) {
	 if (l > r)
	 return 0;
	 if (l == tl && r == tr)
	 return sum[v];
	 int tm = (tl + tr) / 2;
	 return Summa (v*2, tl, tm, l, min(r,tm))
	+ Summa (v*2+1, tm+1, tr, max(l,tm+1), r);
}
int main() {
	int n, t, Possible_Sum, count = 0;
	cin >> t;
	for(int k = 0; k < t; k++) {
		cin >> n;
		int b[n];
		for(int i = 0; i < n; i++) {
			cin >> b[i];
		}
		build(0, n - 1, b, 1);
		bool Array_of_Possible_Sum[8001] = {0};
		for(int i = 0; i < n; i++) {
			for(int j = i + 1; j < n; j++) {
				Possible_Sum = Summa(1,0,n-1,i,j);
				Array_of_Possible_Sum[Possible_Sum] = true;
//				cout << "Possible_Sum " << Possible_Sum << " " << i << " " << j << endl;
			}
		}
		count = 0;
		for(int i = 0; i < n; i++) {
			if(Array_of_Possible_Sum[b[i]])count++;
		}
		cout << count << endl;
	}
	return 0;
}


#include <iostream>
using namespace std;
int main() {
	int t, n;
	cin >> t;
	for(int i = 0; i < t; i++) {
		cin >> n;
		int a[n + 2] = {0};
		bool Possible_Sums[8001] = {0};
		for(int j = 0; j < n; j++) {
			cin >> a[j];
		}
		int sum, left;
		for(int j = 0; j < n; j++) {
			left = 0;
			sum = 0;
			for(int k = 0; k <= j + 1; k++) {
				sum += a[k];
			}
			Possible_Sums[sum] = true;
			for(int k = j + 2; k < n; k++){
				sum -= a[left++];
				sum += a[k];
				Possible_Sums[sum] = true;
			}
		}
		int count = 0;
		for(int j = 0; j < n; j++) {
			if(Possible_Sums[a[j]]){
				count++;
//				cout << a[j] << endl;
			}
		}
		cout << count << endl;
	}
	return 0;
}


#include <iostream>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	int t, n;
	cin >> t;
	for(int i = 0; i < t; i++) {
		cin >> n;
		int a[n];
		bool Possible_Sums[8001] = {0};
		for(int j = 0; j < n; j++) {
			cin >> a[j];
		}
		int sum, left;
		for(int j = 1; j < n; j++) {
			left = 0;
			sum = 0;
			for(int k = 0; k < j + 1; k++) {
				sum += a[k];
			}
			Possible_Sums[sum] = true;
			for(int k = j + 1; k < n; k++){
				sum -= a[left++];
				sum += a[k];
				if(sum > n) continue;
				Possible_Sums[sum] = true;
			}
		}
		int count = 0;
		for(int j = 0; j < n; j++) {
			if(Possible_Sums[a[j]]){
				count++;
//				cout << a[j] << endl;
			}
		}
		cout << count << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define endl  "\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;

void rBuild(vector<ll>  &arr,vector<ll> &tree, ll l,ll r,ll treenode)
{
    if(l == r)
    {
        tree[treenode] = arr[l];
        return ;
    }
    int mid = (l+r) / 2;
    rBuild(arr,tree,l,mid,2*treenode);
    rBuild(arr,tree,mid+1,r,2*treenode+1);
    tree[treenode] = tree[2*treenode] + tree[2*treenode+1];
}

void update(vector <int> &tree,int l, int r,int diff,int ind)
{
    int curInd = 1;
    if(diff == 1)
        tree[curInd] += 1;
    else if(diff == 0)
    {
        if(tree[curInd] != 0)
            tree[curInd] -= 1;
    }

    while(l != r)
    {
        int mid = (l+r)/2;
        if (ind<=mid)
        {
            curInd *= 2;
            r = mid;
        }
        else
        {
            curInd = curInd * 2 + 1;
            l = mid+1;
        }

        if(diff == 1)
            tree[curInd] += 1;
        else if(diff == 0)
        {
            if(tree[curInd] != 0)
                tree[curInd] -= 1;
        }
    }
}

void rUpdate(vector <ll> &tree, ll st,ll end,ll ind,ll curNode)
{
    if(ind <st || ind  > end)
        return;

    tree[curNode] -= 1;
    if(st == end)
        return;

    ll mid = (st+end)/2;
    rUpdate(tree,st,mid,ind,2*curNode);
    rUpdate(tree,mid+1,end,ind,2*curNode+1);
}

int smRange(vector <ll> &arr,ll x,ll y)
{
    ll sm = 0;
    for(ll i=x;i<=y;i++)
    {
        sm += arr[i];
    }
    return  sm;
}

void rRangeSum(vector <ll> &tree,ll l,ll r,ll st,ll en,ll treenode,ll &sum)
{
    if(l<= st && en <= r)
    {
        sum += tree[treenode];
        return;
    }

    ll mid = (st+en)/2;
    if(mid >= l)
    {
        rRangeSum(tree,l,r,st,mid,2*treenode,sum);
    }

    if(r > mid)
    {
        rRangeSum(tree,l,r,mid+1,en,2*treenode + 1,sum);
    }
}

ll query(vector<ll> &tree, ll index, int s, int e, int qs, int qe)
{

    if ( qs > e || qe < s)
        return 0;

    if (qs <= s && qe >= e)
        return tree[index];

    int mid = (s + e)/2;
    return query(tree, 2*index, s, mid, qs, qe) + query(tree, 2*index+1, mid+1, e, qs, qe);

}

ll sumQuery(vector<ll> &tree, ll index, int s, int e, int qs, int qe, ll sum)
{

    if(tree[index] == sum)
        return -1*e -1;

    if(tree[index] < sum)
        return sum - tree[index];

    if(s == e  )
    {
        if(tree[index] - sum > 0)
            return  tree[index] - sum;
        else
            return  sum;
    }

    int mid = ( s + e ) / 2;
    sum = sumQuery(tree, 2*index, s, mid, qs, qe,sum);
    if(sum > 0)
        sum = sumQuery(tree, 2*index+1, mid+1, e, qs, qe,sum);
    return  sum;
}

ll fastExponential(ll b,ll p)
{
    ll res = 1;
    while(p > 0)
    {
        if(p & 1)
        {
            res = res*b;
        }
        b = b*b;
        p = p>>1;
    }
    return res;
}

ll ModularExponential(ll b,ll p,ll mod)
{
    if(p==0)
        return 1;
    if(p == 1)
        return  b%mod;

    ll res = 1;

    while(p > 1)
    {
        if(p & 1)
        {
            res = ((res%mod) * (b%mod))%mod;
            p--;
        }
        else {
            b = (b%mod * b%mod)%mod;
            p = p / 2;
        }
    }

    return ((b%mod)*(res%mod))%mod;
}

ll calcCommanPrefix(vector<string> &arr,int i,int j)
{
    ll sm = 0;
    ll st= i;
    ll en = j;
    ll ind = 0;
    char ch = arr[i][0];
    while(st<en)
    {
        if(ind >= arr[st].size())
            break;
        if(arr[st][ind] != ch)
            break;
        st++;
        if(st == en)
        {
            ind++;
            st = i;
            ch = arr[st][ind];
            sm++;
        }
    }
    return   sm;
}

ll calcBitDiff(ll x,ll y)
{
    ll sm = 0;
    ll st = 1;
    while(x>0 || y>0)
    {
        if((x&1) != (y&1))
            sm += st;
        st = st*2;
        x = x>>1;
        y = y>>1;
    }
    return  sm;
}

int main()
{
    fast
    int t;
    cin>>t;

    while(t--)
    {
        int n,ans = 0;
        cin>>n;
        vector <int> arr(n);
        int mx = 0;
        for(int i=0;i<n;i++) {
            cin >> arr[i];
            mx = max((int)arr[i],mx);
        }

        bitset <8080> dp;

        for(int i=0;i<n-1;i++) {
            int sm = arr[i];
            for (int j = i + 1; j < n; j++) {
                sm += arr[j];
                if(sm>mx)
                    break;
                else
                    dp[sm] = 1;
            }
        }

        for(int i=0;i<n;i++) ans += dp[arr[i]];
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define endl  "\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;

void rBuild(vector<ll>  &arr,vector<ll> &tree, ll l,ll r,ll treenode)
{
    if(l == r)
    {
        tree[treenode] = arr[l];
        return ;
    }
    int mid = (l+r) / 2;
    rBuild(arr,tree,l,mid,2*treenode);
    rBuild(arr,tree,mid+1,r,2*treenode+1);
    tree[treenode] = tree[2*treenode] + tree[2*treenode+1];
}

void update(vector <int> &tree,int l, int r,int diff,int ind)
{
    int curInd = 1;
    if(diff == 1)
        tree[curInd] += 1;
    else if(diff == 0)
    {
        if(tree[curInd] != 0)
            tree[curInd] -= 1;
    }

    while(l != r)
    {
        int mid = (l+r)/2;
        if (ind<=mid)
        {
            curInd *= 2;
            r = mid;
        }
        else
        {
            curInd = curInd * 2 + 1;
            l = mid+1;
        }

        if(diff == 1)
            tree[curInd] += 1;
        else if(diff == 0)
        {
            if(tree[curInd] != 0)
                tree[curInd] -= 1;
        }
    }
}

void rUpdate(vector <ll> &tree, ll st,ll end,ll ind,ll curNode)
{
    if(ind <st || ind  > end)
        return;

    tree[curNode] -= 1;
    if(st == end)
        return;

    ll mid = (st+end)/2;
    rUpdate(tree,st,mid,ind,2*curNode);
    rUpdate(tree,mid+1,end,ind,2*curNode+1);
}

int smRange(vector <ll> &arr,ll x,ll y)
{
    ll sm = 0;
    for(ll i=x;i<=y;i++)
    {
        sm += arr[i];
    }
    return  sm;
}

void rRangeSum(vector <ll> &tree,ll l,ll r,ll st,ll en,ll treenode,ll &sum)
{
    if(l<= st && en <= r)
    {
        sum += tree[treenode];
        return;
    }

    ll mid = (st+en)/2;
    if(mid >= l)
    {
        rRangeSum(tree,l,r,st,mid,2*treenode,sum);
    }

    if(r > mid)
    {
        rRangeSum(tree,l,r,mid+1,en,2*treenode + 1,sum);
    }
}

ll query(vector<ll> &tree, ll index, int s, int e, int qs, int qe)
{

    if ( qs > e || qe < s)
        return 0;

    if (qs <= s && qe >= e)
        return tree[index];

    int mid = (s + e)/2;
    return query(tree, 2*index, s, mid, qs, qe) + query(tree, 2*index+1, mid+1, e, qs, qe);

}

ll sumQuery(vector<ll> &tree, ll index, int s, int e, int qs, int qe, ll sum)
{

    if(tree[index] == sum)
        return -1*e -1;

    if(tree[index] < sum)
        return sum - tree[index];

    if(s == e  )
    {
        if(tree[index] - sum > 0)
            return  tree[index] - sum;
        else
            return  sum;
    }

    int mid = ( s + e ) / 2;
    sum = sumQuery(tree, 2*index, s, mid, qs, qe,sum);
    if(sum > 0)
        sum = sumQuery(tree, 2*index+1, mid+1, e, qs, qe,sum);
    return  sum;
}

ll fastExponential(ll b,ll p)
{
    ll res = 1;
    while(p > 0)
    {
        if(p & 1)
        {
            res = res*b;
        }
        b = b*b;
        p = p>>1;
    }
    return res;
}

ll ModularExponential(ll b,ll p,ll mod)
{
    if(p==0)
        return 1;
    if(p == 1)
        return  b%mod;

    ll res = 1;

    while(p > 1)
    {
        if(p & 1)
        {
            res = ((res%mod) * (b%mod))%mod;
            p--;
        }
        else {
            b = (b%mod * b%mod)%mod;
            p = p / 2;
        }
    }

    return ((b%mod)*(res%mod))%mod;
}

ll calcCommanPrefix(vector<string> &arr,int i,int j)
{
    ll sm = 0;
    ll st= i;
    ll en = j;
    ll ind = 0;
    char ch = arr[i][0];
    while(st<en)
    {
        if(ind >= arr[st].size())
            break;
        if(arr[st][ind] != ch)
            break;
        st++;
        if(st == en)
        {
            ind++;
            st = i;
            ch = arr[st][ind];
            sm++;
        }
    }
    return   sm;
}

ll calcBitDiff(ll x,ll y)
{
    ll sm = 0;
    ll st = 1;
    while(x>0 || y>0)
    {
        if((x&1) != (y&1))
            sm += st;
        st = st*2;
        x = x>>1;
        y = y>>1;
    }
    return  sm;
}

int main()
{
    fast
    int t;
    cin>>t;

    while(t--)
    {
        int n,ans = 0;
        cin>>n;
        vector <int> arr(n);
        int mx = 0;
        for(int i=0;i<n;i++) {
            cin >> arr[i];
            mx = max((int)arr[i],mx);
        }

        bool dp[n+1];

        memset(dp,0, sizeof(dp));

        for(int i=0;i<n-1;i++) {
            int sm = arr[i];
            for (int j = i + 1; j < n; j++) {
                sm += arr[j];
                if(sm>mx)
                    break;
                else
                    dp[sm] = 1;
            }
        }

        for(int i=0;i<n;i++) ans += dp[arr[i]];
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define endl  "\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;

void rBuild(vector<ll>  &arr,vector<ll> &tree, ll l,ll r,ll treenode)
{
    if(l == r)
    {
        tree[treenode] = arr[l];
        return ;
    }
    int mid = (l+r) / 2;
    rBuild(arr,tree,l,mid,2*treenode);
    rBuild(arr,tree,mid+1,r,2*treenode+1);
    tree[treenode] = tree[2*treenode] + tree[2*treenode+1];
}

void update(vector <int> &tree,int l, int r,int diff,int ind)
{
    int curInd = 1;
    if(diff == 1)
        tree[curInd] += 1;
    else if(diff == 0)
    {
        if(tree[curInd] != 0)
            tree[curInd] -= 1;
    }

    while(l != r)
    {
        int mid = (l+r)/2;
        if (ind<=mid)
        {
            curInd *= 2;
            r = mid;
        }
        else
        {
            curInd = curInd * 2 + 1;
            l = mid+1;
        }

        if(diff == 1)
            tree[curInd] += 1;
        else if(diff == 0)
        {
            if(tree[curInd] != 0)
                tree[curInd] -= 1;
        }
    }
}

void rUpdate(vector <ll> &tree, ll st,ll end,ll ind,ll curNode)
{
    if(ind <st || ind  > end)
        return;

    tree[curNode] -= 1;
    if(st == end)
        return;

    ll mid = (st+end)/2;
    rUpdate(tree,st,mid,ind,2*curNode);
    rUpdate(tree,mid+1,end,ind,2*curNode+1);
}

int smRange(vector <ll> &arr,ll x,ll y)
{
    ll sm = 0;
    for(ll i=x;i<=y;i++)
    {
        sm += arr[i];
    }
    return  sm;
}

void rRangeSum(vector <ll> &tree,ll l,ll r,ll st,ll en,ll treenode,ll &sum)
{
    if(l<= st && en <= r)
    {
        sum += tree[treenode];
        return;
    }

    ll mid = (st+en)/2;
    if(mid >= l)
    {
        rRangeSum(tree,l,r,st,mid,2*treenode,sum);
    }

    if(r > mid)
    {
        rRangeSum(tree,l,r,mid+1,en,2*treenode + 1,sum);
    }
}

ll query(vector<ll> &tree, ll index, int s, int e, int qs, int qe)
{

    if ( qs > e || qe < s)
        return 0;

    if (qs <= s && qe >= e)
        return tree[index];

    int mid = (s + e)/2;
    return query(tree, 2*index, s, mid, qs, qe) + query(tree, 2*index+1, mid+1, e, qs, qe);

}

ll sumQuery(vector<ll> &tree, ll index, int s, int e, int qs, int qe, ll sum)
{

    if(tree[index] == sum)
        return -1*e -1;

    if(tree[index] < sum)
        return sum - tree[index];

    if(s == e  )
    {
        if(tree[index] - sum > 0)
            return  tree[index] - sum;
        else
            return  sum;
    }

    int mid = ( s + e ) / 2;
    sum = sumQuery(tree, 2*index, s, mid, qs, qe,sum);
    if(sum > 0)
        sum = sumQuery(tree, 2*index+1, mid+1, e, qs, qe,sum);
    return  sum;
}

ll fastExponential(ll b,ll p)
{
    ll res = 1;
    while(p > 0)
    {
        if(p & 1)
        {
            res = res*b;
        }
        b = b*b;
        p = p>>1;
    }
    return res;
}

ll ModularExponential(ll b,ll p,ll mod)
{
    if(p==0)
        return 1;
    if(p == 1)
        return  b%mod;

    ll res = 1;

    while(p > 1)
    {
        if(p & 1)
        {
            res = ((res%mod) * (b%mod))%mod;
            p--;
        }
        else {
            b = (b%mod * b%mod)%mod;
            p = p / 2;
        }
    }

    return ((b%mod)*(res%mod))%mod;
}

ll calcCommanPrefix(vector<string> &arr,int i,int j)
{
    ll sm = 0;
    ll st= i;
    ll en = j;
    ll ind = 0;
    char ch = arr[i][0];
    while(st<en)
    {
        if(ind >= arr[st].size())
            break;
        if(arr[st][ind] != ch)
            break;
        st++;
        if(st == en)
        {
            ind++;
            st = i;
            ch = arr[st][ind];
            sm++;
        }
    }
    return   sm;
}

ll calcBitDiff(ll x,ll y)
{
    ll sm = 0;
    ll st = 1;
    while(x>0 || y>0)
    {
        if((x&1) != (y&1))
            sm += st;
        st = st*2;
        x = x>>1;
        y = y>>1;
    }
    return  sm;
}

int main()
{
    fast
    ll t;
    cin>>t;

    while(t--)
    {
        int n,ans = 0;
        cin>>n;
        vector <int> arr(n);
        int mx = 0;
        for(int i=0;i<n;i++) {
            cin >> arr[i];
            mx = max((int)arr[i],mx);
        }

        bitset <8080> dp;

        for(int i=0;i<n-1;i++) {
            int sm = arr[i];
            for (int j = i + 1; j < n; j++) {
                sm += arr[j];
                if(sm>mx)
                    break;
                else
                    dp[sm] = 1;
            }
        }

        for(int i=0;i<n;i++) ans += dp[arr[i]];
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define endl  "\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;

void rBuild(vector<ll>  &arr,vector<ll> &tree, ll l,ll r,ll treenode)
{
    if(l == r)
    {
        tree[treenode] = arr[l];
        return ;
    }
    int mid = (l+r) / 2;
    rBuild(arr,tree,l,mid,2*treenode);
    rBuild(arr,tree,mid+1,r,2*treenode+1);
    tree[treenode] = tree[2*treenode] + tree[2*treenode+1];
}

void update(vector <int> &tree,int l, int r,int diff,int ind)
{
    int curInd = 1;
    if(diff == 1)
        tree[curInd] += 1;
    else if(diff == 0)
    {
        if(tree[curInd] != 0)
            tree[curInd] -= 1;
    }

    while(l != r)
    {
        int mid = (l+r)/2;
        if (ind<=mid)
        {
            curInd *= 2;
            r = mid;
        }
        else
        {
            curInd = curInd * 2 + 1;
            l = mid+1;
        }

        if(diff == 1)
            tree[curInd] += 1;
        else if(diff == 0)
        {
            if(tree[curInd] != 0)
                tree[curInd] -= 1;
        }
    }
}

void rUpdate(vector <ll> &tree, ll st,ll end,ll ind,ll curNode)
{
    if(ind <st || ind  > end)
        return;

    tree[curNode] -= 1;
    if(st == end)
        return;

    ll mid = (st+end)/2;
    rUpdate(tree,st,mid,ind,2*curNode);
    rUpdate(tree,mid+1,end,ind,2*curNode+1);
}

int smRange(vector <ll> &arr,ll x,ll y)
{
    ll sm = 0;
    for(ll i=x;i<=y;i++)
    {
        sm += arr[i];
    }
    return  sm;
}

void rRangeSum(vector <ll> &tree,ll l,ll r,ll st,ll en,ll treenode,ll &sum)
{
    if(l<= st && en <= r)
    {
        sum += tree[treenode];
        return;
    }

    ll mid = (st+en)/2;
    if(mid >= l)
    {
        rRangeSum(tree,l,r,st,mid,2*treenode,sum);
    }

    if(r > mid)
    {
        rRangeSum(tree,l,r,mid+1,en,2*treenode + 1,sum);
    }
}

ll query(vector<ll> &tree, ll index, int s, int e, int qs, int qe)
{

    if ( qs > e || qe < s)
        return 0;

    if (qs <= s && qe >= e)
        return tree[index];

    int mid = (s + e)/2;
    return query(tree, 2*index, s, mid, qs, qe) + query(tree, 2*index+1, mid+1, e, qs, qe);

}

ll sumQuery(vector<ll> &tree, ll index, int s, int e, int qs, int qe, ll sum)
{

    if(tree[index] == sum)
        return -1*e -1;

    if(tree[index] < sum)
        return sum - tree[index];

    if(s == e  )
    {
        if(tree[index] - sum > 0)
            return  tree[index] - sum;
        else
            return  sum;
    }

    int mid = ( s + e ) / 2;
    sum = sumQuery(tree, 2*index, s, mid, qs, qe,sum);
    if(sum > 0)
        sum = sumQuery(tree, 2*index+1, mid+1, e, qs, qe,sum);
    return  sum;
}

ll fastExponential(ll b,ll p)
{
    ll res = 1;
    while(p > 0)
    {
        if(p & 1)
        {
            res = res*b;
        }
        b = b*b;
        p = p>>1;
    }
    return res;
}

ll ModularExponential(ll b,ll p,ll mod)
{
    if(p==0)
        return 1;
    if(p == 1)
        return  b%mod;

    ll res = 1;

    while(p > 1)
    {
        if(p & 1)
        {
            res = ((res%mod) * (b%mod))%mod;
            p--;
        }
        else {
            b = (b%mod * b%mod)%mod;
            p = p / 2;
        }
    }

    return ((b%mod)*(res%mod))%mod;
}

ll calcCommanPrefix(vector<string> &arr,int i,int j)
{
    ll sm = 0;
    ll st= i;
    ll en = j;
    ll ind = 0;
    char ch = arr[i][0];
    while(st<en)
    {
        if(ind >= arr[st].size())
            break;
        if(arr[st][ind] != ch)
            break;
        st++;
        if(st == en)
        {
            ind++;
            st = i;
            ch = arr[st][ind];
            sm++;
        }
    }
    return   sm;
}

ll calcBitDiff(ll x,ll y)
{
    ll sm = 0;
    ll st = 1;
    while(x>0 || y>0)
    {
        if((x&1) != (y&1))
            sm += st;
        st = st*2;
        x = x>>1;
        y = y>>1;
    }
    return  sm;
}

int main()
{
    //fast
    ll t;
    cin>>t;

    while(t--)
    {
        ll n,ans = 0;
        cin>>n;
        vector <ll> arr(n);
        for(ll i=0;i<n;i++)
            cin>>arr[i];

        bitset <8080> dp;

        for(ll i=0;i<n-1;i++) {
            ll sm = arr[i];
            for (ll j = i + 1; j < n; j++) {
                sm += arr[j];
                if(sm>n)
                    break;
                else
                    dp[sm] = 1;
            }
        }

        for(ll i=0;i<n;i++) ans += dp[arr[i]];
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define endl  "\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;

void rBuild(vector<ll>  &arr,vector<ll> &tree, ll l,ll r,ll treenode)
{
    if(l == r)
    {
        tree[treenode] = arr[l];
        return ;
    }
    int mid = (l+r) / 2;
    rBuild(arr,tree,l,mid,2*treenode);
    rBuild(arr,tree,mid+1,r,2*treenode+1);
    tree[treenode] = tree[2*treenode] + tree[2*treenode+1];
}

void update(vector <int> &tree,int l, int r,int diff,int ind)
{
    int curInd = 1;
    if(diff == 1)
        tree[curInd] += 1;
    else if(diff == 0)
    {
        if(tree[curInd] != 0)
            tree[curInd] -= 1;
    }

    while(l != r)
    {
        int mid = (l+r)/2;
        if (ind<=mid)
        {
            curInd *= 2;
            r = mid;
        }
        else
        {
            curInd = curInd * 2 + 1;
            l = mid+1;
        }

        if(diff == 1)
            tree[curInd] += 1;
        else if(diff == 0)
        {
            if(tree[curInd] != 0)
                tree[curInd] -= 1;
        }
    }
}

void rUpdate(vector <ll> &tree, ll st,ll end,ll ind,ll curNode)
{
    if(ind <st || ind  > end)
        return;

    tree[curNode] -= 1;
    if(st == end)
        return;

    ll mid = (st+end)/2;
    rUpdate(tree,st,mid,ind,2*curNode);
    rUpdate(tree,mid+1,end,ind,2*curNode+1);
}

int smRange(vector <ll> &arr,ll x,ll y)
{
    ll sm = 0;
    for(ll i=x;i<=y;i++)
    {
        sm += arr[i];
    }
    return  sm;
}

void rRangeSum(vector <ll> &tree,ll l,ll r,ll st,ll en,ll treenode,ll &sum)
{
    if(l<= st && en <= r)
    {
        sum += tree[treenode];
        return;
    }

    ll mid = (st+en)/2;
    if(mid >= l)
    {
        rRangeSum(tree,l,r,st,mid,2*treenode,sum);
    }

    if(r > mid)
    {
        rRangeSum(tree,l,r,mid+1,en,2*treenode + 1,sum);
    }
}

ll query(vector<ll> &tree, ll index, int s, int e, int qs, int qe)
{

    if ( qs > e || qe < s)
        return 0;

    if (qs <= s && qe >= e)
        return tree[index];

    int mid = (s + e)/2;
    return query(tree, 2*index, s, mid, qs, qe) + query(tree, 2*index+1, mid+1, e, qs, qe);

}

ll sumQuery(vector<ll> &tree, ll index, int s, int e, int qs, int qe, ll sum)
{

    if(tree[index] == sum)
        return -1*e -1;

    if(tree[index] < sum)
        return sum - tree[index];

    if(s == e  )
    {
        if(tree[index] - sum > 0)
            return  tree[index] - sum;
        else
            return  sum;
    }

    int mid = ( s + e ) / 2;
    sum = sumQuery(tree, 2*index, s, mid, qs, qe,sum);
    if(sum > 0)
        sum = sumQuery(tree, 2*index+1, mid+1, e, qs, qe,sum);
    return  sum;
}

ll fastExponential(ll b,ll p)
{
    ll res = 1;
    while(p > 0)
    {
        if(p & 1)
        {
            res = res*b;
        }
        b = b*b;
        p = p>>1;
    }
    return res;
}

ll ModularExponential(ll b,ll p,ll mod)
{
    if(p==0)
        return 1;
    if(p == 1)
        return  b%mod;

    ll res = 1;

    while(p > 1)
    {
        if(p & 1)
        {
            res = ((res%mod) * (b%mod))%mod;
            p--;
        }
        else {
            b = (b%mod * b%mod)%mod;
            p = p / 2;
        }
    }

    return ((b%mod)*(res%mod))%mod;
}

ll calcCommanPrefix(vector<string> &arr,int i,int j)
{
    ll sm = 0;
    ll st= i;
    ll en = j;
    ll ind = 0;
    char ch = arr[i][0];
    while(st<en)
    {
        if(ind >= arr[st].size())
            break;
        if(arr[st][ind] != ch)
            break;
        st++;
        if(st == en)
        {
            ind++;
            st = i;
            ch = arr[st][ind];
            sm++;
        }
    }
    return   sm;
}

ll calcBitDiff(ll x,ll y)
{
    ll sm = 0;
    ll st = 1;
    while(x>0 || y>0)
    {
        if((x&1) != (y&1))
            sm += st;
        st = st*2;
        x = x>>1;
        y = y>>1;
    }
    return  sm;
}

int main()
{
    fast
    ll t;
    cin>>t;

    while(t--)
    {
        ll n,ans = 0;
        cin>>n;
        vector <ll> arr(n);
        for(ll i=0;i<n;i++)
            cin>>arr[i];

        bitset <8080> dp;

        for(ll i=0;i<n-1;i++) {
            ll sm = arr[i];
            for (ll j = i + 1; j < n; j++) {
                sm += arr[j];
                if(sm>n)
                    break;
                else
                    dp[sm] = 1;
            }
        }

        for(ll i=0;i<n;i++) ans += dp[arr[i]];
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define endl  "\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;

void rBuild(vector<ll>  &arr,vector<ll> &tree, ll l,ll r,ll treenode)
{
    if(l == r)
    {
        tree[treenode] = arr[l];
        return ;
    }
    int mid = (l+r) / 2;
    rBuild(arr,tree,l,mid,2*treenode);
    rBuild(arr,tree,mid+1,r,2*treenode+1);
    tree[treenode] = tree[2*treenode] + tree[2*treenode+1];
}

void update(vector <int> &tree,int l, int r,int diff,int ind)
{
    int curInd = 1;
    if(diff == 1)
        tree[curInd] += 1;
    else if(diff == 0)
    {
        if(tree[curInd] != 0)
            tree[curInd] -= 1;
    }

    while(l != r)
    {
        int mid = (l+r)/2;
        if (ind<=mid)
        {
            curInd *= 2;
            r = mid;
        }
        else
        {
            curInd = curInd * 2 + 1;
            l = mid+1;
        }

        if(diff == 1)
            tree[curInd] += 1;
        else if(diff == 0)
        {
            if(tree[curInd] != 0)
                tree[curInd] -= 1;
        }
    }
}

void rUpdate(vector <ll> &tree, ll st,ll end,ll ind,ll curNode)
{
    if(ind <st || ind  > end)
        return;

    tree[curNode] -= 1;
    if(st == end)
        return;

    ll mid = (st+end)/2;
    rUpdate(tree,st,mid,ind,2*curNode);
    rUpdate(tree,mid+1,end,ind,2*curNode+1);
}

int smRange(vector <ll> &arr,ll x,ll y)
{
    ll sm = 0;
    for(ll i=x;i<=y;i++)
    {
        sm += arr[i];
    }
    return  sm;
}

void rRangeSum(vector <ll> &tree,ll l,ll r,ll st,ll en,ll treenode,ll &sum)
{
    if(l<= st && en <= r)
    {
        sum += tree[treenode];
        return;
    }

    ll mid = (st+en)/2;
    if(mid >= l)
    {
        rRangeSum(tree,l,r,st,mid,2*treenode,sum);
    }

    if(r > mid)
    {
        rRangeSum(tree,l,r,mid+1,en,2*treenode + 1,sum);
    }
}

ll query(vector<ll> &tree, ll index, int s, int e, int qs, int qe)
{

    if ( qs > e || qe < s)
        return 0;

    if (qs <= s && qe >= e)
        return tree[index];

    int mid = (s + e)/2;
    return query(tree, 2*index, s, mid, qs, qe) + query(tree, 2*index+1, mid+1, e, qs, qe);

}

ll sumQuery(vector<ll> &tree, ll index, int s, int e, int qs, int qe, ll sum)
{

    if(tree[index] == sum)
        return -1*e -1;

    if(tree[index] < sum)
        return sum - tree[index];

    if(s == e  )
    {
        if(tree[index] - sum > 0)
            return  tree[index] - sum;
        else
            return  sum;
    }

    int mid = ( s + e ) / 2;
    sum = sumQuery(tree, 2*index, s, mid, qs, qe,sum);
    if(sum > 0)
        sum = sumQuery(tree, 2*index+1, mid+1, e, qs, qe,sum);
    return  sum;
}

ll fastExponential(ll b,ll p)
{
    ll res = 1;
    while(p > 0)
    {
        if(p & 1)
        {
            res = res*b;
        }
        b = b*b;
        p = p>>1;
    }
    return res;
}

ll ModularExponential(ll b,ll p,ll mod)
{
    if(p==0)
        return 1;
    if(p == 1)
        return  b%mod;

    ll res = 1;

    while(p > 1)
    {
        if(p & 1)
        {
            res = ((res%mod) * (b%mod))%mod;
            p--;
        }
        else {
            b = (b%mod * b%mod)%mod;
            p = p / 2;
        }
    }

    return ((b%mod)*(res%mod))%mod;
}

ll calcCommanPrefix(vector<string> &arr,int i,int j)
{
    ll sm = 0;
    ll st= i;
    ll en = j;
    ll ind = 0;
    char ch = arr[i][0];
    while(st<en)
    {
        if(ind >= arr[st].size())
            break;
        if(arr[st][ind] != ch)
            break;
        st++;
        if(st == en)
        {
            ind++;
            st = i;
            ch = arr[st][ind];
            sm++;
        }
    }
    return   sm;
}

ll calcBitDiff(ll x,ll y)
{
    ll sm = 0;
    ll st = 1;
    while(x>0 || y>0)
    {
        if((x&1) != (y&1))
            sm += st;
        st = st*2;
        x = x>>1;
        y = y>>1;
    }
    return  sm;
}

int main()
{
    fast
    ll t;
    cin>>t;

    while(t--)
    {
        ll n,ans = 0;
        cin>>n;
        ll arr[n];
        memset(arr,0, sizeof(arr));
        ll mx = 0;
        for(ll i=0;i<n;i++) {
            cin >> arr[i];
            mx = max(arr[i],mx);
        }

        bitset <8080> dp;

        for(ll i=0;i<n-1;i++) {
            ll sm = arr[i];
            for (ll j = i + 1; j < n; j++) {
                sm += arr[j];
                if(sm>mx)
                    break;
                else
                    dp[sm] = 1;
            }
        }

        for(ll i=0;i<n;i++) ans += dp[arr[i]];
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
#define ll long long int
#define endl  "\n"
#define fast ios_base::sync_with_stdio(false); cin.tie(0);
using namespace std;

void rBuild(vector<ll>  &arr,vector<ll> &tree, ll l,ll r,ll treenode)
{
    if(l == r)
    {
        tree[treenode] = arr[l];
        return ;
    }
    int mid = (l+r) / 2;
    rBuild(arr,tree,l,mid,2*treenode);
    rBuild(arr,tree,mid+1,r,2*treenode+1);
    tree[treenode] = tree[2*treenode] + tree[2*treenode+1];
}

void update(vector <int> &tree,int l, int r,int diff,int ind)
{
    int curInd = 1;
    if(diff == 1)
        tree[curInd] += 1;
    else if(diff == 0)
    {
        if(tree[curInd] != 0)
            tree[curInd] -= 1;
    }

    while(l != r)
    {
        int mid = (l+r)/2;
        if (ind<=mid)
        {
            curInd *= 2;
            r = mid;
        }
        else
        {
            curInd = curInd * 2 + 1;
            l = mid+1;
        }

        if(diff == 1)
            tree[curInd] += 1;
        else if(diff == 0)
        {
            if(tree[curInd] != 0)
                tree[curInd] -= 1;
        }
    }
}

void rUpdate(vector <ll> &tree, ll st,ll end,ll ind,ll curNode)
{
    if(ind <st || ind  > end)
        return;

    tree[curNode] -= 1;
    if(st == end)
        return;

    ll mid = (st+end)/2;
    rUpdate(tree,st,mid,ind,2*curNode);
    rUpdate(tree,mid+1,end,ind,2*curNode+1);
}

int smRange(vector <ll> &arr,ll x,ll y)
{
    ll sm = 0;
    for(ll i=x;i<=y;i++)
    {
        sm += arr[i];
    }
    return  sm;
}

void rRangeSum(vector <ll> &tree,ll l,ll r,ll st,ll en,ll treenode,ll &sum)
{
    if(l<= st && en <= r)
    {
        sum += tree[treenode];
        return;
    }

    ll mid = (st+en)/2;
    if(mid >= l)
    {
        rRangeSum(tree,l,r,st,mid,2*treenode,sum);
    }

    if(r > mid)
    {
        rRangeSum(tree,l,r,mid+1,en,2*treenode + 1,sum);
    }
}

ll query(vector<ll> &tree, ll index, int s, int e, int qs, int qe)
{

    if ( qs > e || qe < s)
        return 0;

    if (qs <= s && qe >= e)
        return tree[index];

    int mid = (s + e)/2;
    return query(tree, 2*index, s, mid, qs, qe) + query(tree, 2*index+1, mid+1, e, qs, qe);

}

ll sumQuery(vector<ll> &tree, ll index, int s, int e, int qs, int qe, ll sum)
{

    if(tree[index] == sum)
        return -1*e -1;

    if(tree[index] < sum)
        return sum - tree[index];

    if(s == e  )
    {
        if(tree[index] - sum > 0)
            return  tree[index] - sum;
        else
            return  sum;
    }

    int mid = ( s + e ) / 2;
    sum = sumQuery(tree, 2*index, s, mid, qs, qe,sum);
    if(sum > 0)
        sum = sumQuery(tree, 2*index+1, mid+1, e, qs, qe,sum);
    return  sum;
}

ll fastExponential(ll b,ll p)
{
    ll res = 1;
    while(p > 0)
    {
        if(p & 1)
        {
            res = res*b;
        }
        b = b*b;
        p = p>>1;
    }
    return res;
}

ll ModularExponential(ll b,ll p,ll mod)
{
    if(p==0)
        return 1;
    if(p == 1)
        return  b%mod;

    ll res = 1;

    while(p > 1)
    {
        if(p & 1)
        {
            res = ((res%mod) * (b%mod))%mod;
            p--;
        }
        else {
            b = (b%mod * b%mod)%mod;
            p = p / 2;
        }
    }

    return ((b%mod)*(res%mod))%mod;
}

ll calcCommanPrefix(vector<string> &arr,int i,int j)
{
    ll sm = 0;
    ll st= i;
    ll en = j;
    ll ind = 0;
    char ch = arr[i][0];
    while(st<en)
    {
        if(ind >= arr[st].size())
            break;
        if(arr[st][ind] != ch)
            break;
        st++;
        if(st == en)
        {
            ind++;
            st = i;
            ch = arr[st][ind];
            sm++;
        }
    }
    return   sm;
}

ll calcBitDiff(ll x,ll y)
{
    ll sm = 0;
    ll st = 1;
    while(x>0 || y>0)
    {
        if((x&1) != (y&1))
            sm += st;
        st = st*2;
        x = x>>1;
        y = y>>1;
    }
    return  sm;
}

int main()
{
    fast
    ll t;
    cin>>t;

    while(t--)
    {
        ll n,ans = 0;
        cin>>n;
        vector <int> arr(n);
        ll mx = 0;
        for(ll i=0;i<n;i++) {
            cin >> arr[i];
            mx = max((arr[i],mx);
        }

        bitset <8080> dp;

        for(ll i=0;i<n-1;i++) {
            ll sm = arr[i];
            for (ll j = i + 1; j < n; j++) {
                sm += arr[j];
                if(sm>mx)
                    break;
                else
                    dp[sm] = 1;
            }
        }

        for(ll i=0;i<n;i++) ans += dp[arr[i]];
        cout<<ans<<endl;
    }
}