from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())

for _ in range(ii()):

    n,k=mi()
    s=si()
    ans=0
    '''
    we divided string S into n//k complete word,so i,i+k,i+2*k.....will be same and also
    satisfying palindrome we need to equal the element at position k-i-1,2*k-i-1,3*k-i-1...........
    ____7__ ___7__  ___7___
    wudixia oxingxi ngheclp ->{w,o,n,a,i,p} must be equal so we store that elements in a
    freq array and convert all that elements with the element whose frequency is maximum
    '''
    for i in range(ceil(k/2)):
        x=[0]*26
        for j in range(i,n,k):
            x[ord(s[j])-ord('a')]+=1
        if(i!=(k-i-1)):
            for j in range(k-i-1,n,k):
                x[ord(s[j])-ord('a')]+=1
        s1=sum(x)
        n1=max(x)
        ans+=(s1-n1)
    print(ans)


for _ in " "*int(input()):
    a,b=map(int,input().split());d=input();f=0
    for i in range(b):
        c=[0]*26
        for j in range(i,a,b):
            c[ord(d[j])-97]+=1
            c[ord(d[b-j-1])-97]+=1
        f+=sum(c)-max(c)
    print(f//2)


def check(s,k):
	string = s[0:k]
	if s==s[::-1]:
		for i in range(len(s)//k):
			if s[i*k:(i+1)*k]!=string:
				return False
		return True
	else:
		return False

def calc(s):
	options={}
	i,j,count=0,len(s)-1,0
	while i<=j:
		if s[i]!=s[j]:
			count+=1
		if i!=j:
			options[i]=[s[i],s[j]]
		else:
			options[i]=[s[i]]
		i+=1
		j-=1
	return (options,count)

for nt in range(int(input())):
	n,k=map(int,input().split())
	s=input()
	if check(s,k):
		print (0)
	else:
		freq={}
		ans=0
		for i in range(n//k):
			d,ans=calc(s[i*k:k*(i+1)])
			for j in d:
				for m in d[j]:
					if j in freq:
						if m in freq[j]:
							freq[j][m]+=1
						else:
							freq[j][m]=1
					else:
						freq[j]={m:1}
			# print (d)
			# print (ans)
		ans=0
		for i in freq:
			s=0
			m=0
			for j in freq[i]:
				s+=freq[i][j]
				if freq[i][j]>m:
					m=freq[i][j]
			ans+=s-m
		# print (freq)
		print (ans)


def check(s,k):
	string = s[0:k]
	if s==s[::-1]:
		for i in range(len(s)//k):
			if s[i*k:(i+1)*k]!=string:
				return False
		return True
	else:
		return False

def calc(s):
	options={}
	i,j,count=0,len(s)-1,0
	while i<=j:
		if s[i]!=s[j]:
			count+=1
			options[i]=[s[i],s[j]]
		else:
			options[i]=[s[i]]
		i+=1
		j-=1
	return (options,count)

for nt in range(int(input())):
	n,k=map(int,input().split())
	s=input()
	if check(s,k):
		print (0)
	else:
		freq={}
		for i in range(n//k):
			d,ans=calc(s[i*k:k*(i+1)])
			for j in d:
				for m in d[j]:
					if j in freq:
						if m in freq[j]:
							freq[j][m]+=1
						else:
							freq[j][m]=1
					else:
						freq[j]={m:1}
			# print (d)
			# print (ans)
		ans=0
		for i in freq:
			s=0
			m=0
			for j in freq[i]:
				s+=freq[i][j]
				if freq[i][j]>m:
					m=freq[i][j]
			ans+=s-m
		# print (freq)
		print (ans)


t=int(input())
for _ in range(t):
    n,k=map(int,input().split())
    l=list(input())
    ls=[]
    for i in range(26):
        ls.append(0)
    count=0
    if k%2==1:
        i=k//2
        while(i<n):
            ls[ord(l[i])-97]+=1
            i+=k
        maximum=max(ls)
        count=count+n//k-maximum
        z=2
    else:
        z=1
    i=k//2-1
    while(i>=0):
        j=i
        for temp in range(26):
            ls[temp]=0
        while(j<n):
            ls[ord(l[j])-97]+=1
            j+=k
        j=i+z
        while(j<n):
            ls[ord(l[j])-97]+=1
            j+=k
        maximum=max(ls)
        count=count+2*n//k-maximum
        i=i-1
        z=z+2
    print(count)


# python3

def solve(s, k):
    n = len(s)
    r = n//k
    res = 0
    for j in range(k//2):
        d = [0] * 26
        for i in range(r):
            d[ord(s[i*k+j]) - 97] += 1
            d[ord(s[i*k+k-1-j]) - 97] += 1
        res += 2*r - max(d)
    if k%2 == 1:
        d = [0] * 26
        for i in range(r):
            d[ord(s[i*k+k//2]) - 97] += 1
        res += r - max(d)
    return res

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n, k = map(int, input().split())
        s = input()
        print(solve(s, k))


import math,string,itertools,fractions,heapq,collections,re,array,bisect

# Create a function called "chunks" with two arguments, l and n:
def chunks(l, n):
    # For item i in a range that is a length of l,
    for i in range(0, len(l), n):
        # Create an index range for l of n items:
        yield l[i:i+n]

cases = int(input().strip())
for case in range(cases):
  n, k = list(map(int, input().strip().split(" ")))
  s = list(input().strip())
  s = list(chunks(s, k))
  ans = 0
  for i in range(int((k + 1) / 2)):
    d = collections.defaultdict(int)
    most_common = 0
    total = 0
    for j in range(len(s)):
      d[s[j][i]] += 1
      most_common = max(most_common, d[s[j][i]])
      # print(j, i)
      total += 1
      if i < (k - i - 1):
        d[s[j][k - i - 1]] += 1
        most_common = max(most_common, d[s[j][k - i - 1]])
        # print(j, k - i - 1)
        total += 1
    # print(d, total, most_common)
    ans += total - most_common
  print(int(ans))


t = int(input())
for i in range(t):

    n, k = [int(i) for i in input().split()]
    s = input()

    cnt = [[0 for i in range(26)] for j in range(k)]

    for i in range(k):
        for j in range(i, len(s), k):
            cnt[i][ord(s[j])-ord('a')] += 1

    ans = 0
    for i in range(k):

        temp = []
        for j in range(26):
            temp.append(cnt[i][j]+cnt[k-i-1][j])

        ans += sum(temp)-max(temp)
    print(ans//2)


t = int(input())
for i in range(t):

    n, k = [int(i) for i in input().split()]
    s = input()

    ans = 0
    for i in range(k//2):
        cnt = [0 for i in range(26)]
        l = k-i-1
        for j in range(i, len(s), k):
            cnt[ord(s[j])-ord('a')] += 1
        for j in range(l, len(s), k):
            cnt[ord(s[j])-ord('a')] += 1

        ans += sum(cnt)-max(cnt)

    if k%2 == 1:
        i = k//2
        cnt = [0 for i in range(26)]
        for j in range(i, len(s), k):
            cnt[ord(s[j])-ord('a')] += 1

        ans += sum(cnt)-max(cnt)

    print(ans)


from collections import defaultdict
t = int(input())
for i in range(t):

    n, k = [int(i) for i in input().split()]
    s = input()

    cnt = defaultdict(lambda : [0 for i in range(26)])

    for i in range(k):
        for j in range(i, len(s), k):
            cnt[i][ord(s[j])-ord('a')] += 1

    ans = 0
    for i in range(k):

        temp = []
        for j in range(26):
            temp.append(cnt[i][j]+cnt[k-i-1][j])

        ans += sum(temp)-max(temp)

    print(ans//2)


from itertools import groupby
for _ in range(int(input())):
    n,k=map(int,input().split())
    s=input()
    x=n//k
    c=0
    if(k%2!=0):
        k2=k//2+1
    else:
        k2=k//2
    for i in range(k2):
        l=[]
        l1=[]
        for j in range(0,x):
            if(i!=k//2):
                y=j*k+i
                y1=j*k+k-i-1
                l.append(s[y])
                l.append(s[y1])
                l1.append(y)
                l1.append(y1)
            else:
                y=j*k+i
                l.append(s[y])
                l1.append(y)
        l.sort()
        m=0
        for key,group in groupby(l):
            k1=len(list(group))
            if(k1>m):
                m=k1
        c=c+len(l)-m
    print(c)


ans=[]
for s in range(int(input())):
    n,k=[int(x) for x in input().split()]
    s1=input()
    final=0
    for i in range(0,k):
        arr=[0]*26
        j=i
        while j<n:
            arr[ord(s1[j])-97]+=1
            arr[ord(s1[k-1-j])-97]+=1
            j+=k
        final+=sum(arr)-max(arr)
    ans.append(final//2)
for it in ans:
    print(it)


// In the Name of Allah
// AD13
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef unsigned long long  ull;
typedef pair <int, int> pii;
const double eps = 1e-14; // 15 is also used!
const int INF = 2147483647;
//#define For(i, a, b) for (int i = (a); i < (b); i++)
#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << err; while(1); }

/*-------------------------------------------------------------------------------------*/
/*_____________________________________________________________________________________*/

ll arr [5 * 100345];
int zero_until [5 * 100345];

int main () {
	ll n, sum;
	sum = 0;
	cin >> n;
	for (int i=0; i<n; i++) {
		cin >> arr[i];
		sum += arr[i];
	}
	for (int i=0; i<n; i++) {
		if (arr[i] == 0) {
			int j = i+1;
			while (j < n && arr[j] == 0) j++;
			for(int k=j-1; k>=i;k--)
				zero_until[k] = j;
			i = j-1;
		}
	}

	ll cnt = 0;
	ll s1 = 0;
	for (int i=0; i<n-2; i++) {
		s1 += arr[i];
		if (sum != s1 * 3) continue;
		ll s2 = 0;
		for (int j=i+1; j<n-1; j++){
			s2 += arr[j];
			if (s1 == s2) {
				cnt++;
				if (arr[j] == 0) {
					int k = zero_until[j];
					cnt += k-j-1;
					j = k-1;
				}
			}
			else if (arr[j] == 0){
				j = zero_until[j]-1;
			}
		}
	}

	cout << cnt << endl;

	return 0;
}
/*
7
3 3 0 0 0 3 0
--> 4
*/


// In the Name of Allah
// AD13
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef unsigned long long  ull;
typedef pair <int, int> pii;
const double eps = 1e-14; // 15 is also used!
const int INF = 2147483647;
//#define For(i, a, b) for (int i = (a); i < (b); i++)
#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << err; while(1); }

/*-------------------------------------------------------------------------------------*/
/*_____________________________________________________________________________________*/

ll arr [5 * 100345];
int zero_until [5 * 100345];

int main () {
	ll n, sum;
	sum = 0;
	cin >> n;
	for (int i=0; i<n; i++) {
		cin >> arr[i];
		sum += arr[i];
	}
	for (int i=0; i<n; i++) {
		if (arr[i] == 0) {
			int j = i+1;
			while (j < n && arr[j] == 0) j++;
			for(int k=j; k>=i;k--)
				zero_until[k] = j;
			i = j-1;
		}
	}

	ll cnt = 0;
	ll s1 = 0;
	for (int i=0; i<n-2; i++) {
		s1 += arr[i];
		if (sum != s1 * 3) continue;
		ll s2 = 0;
		for (int j=i+1; j<n-1; j++){
			s2 += arr[j];
			if (s1 == s2) {
				cnt++;
				if (arr[j] == 0) {
					int k = zero_until[j];
					cnt += k-j-1;
					j = k-1;
				}
			}
			else if (arr[j] == 0){
				j = zero_until[j]-1;
			}
		}
	}

	cout << cnt << endl;

	return 0;
}
/*
7
3 3 0 0 0 3 0
--> 4
*/


// In the Name of Allah
// AD13
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef unsigned long long  ull;
typedef pair <int, int> pii;
const double eps = 1e-14; // 15 is also used!
const int INF = 2147483647;
//#define For(i, a, b) for (int i = (a); i < (b); i++)
#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << err; while(1); }
/*-------------------------------------------------------------------------------------*/

/*_____________________________________________________________________________________*/

ll arr [5 * 12345];

int main () {
	ll n, sum;
	sum = 0;
	cin >> n;
	for (int i=0; i<n; i++) {
		cin >> arr[i];
		sum += arr[i];
	}

	ll cnt = 0;
	ll s1 = 0;
	for (int i=0; i<n-2; i++) {
		s1 += arr[i];
		ll s2 = 0;
		for (int j=i+1; j<n-1; j++){
			s2 += arr[j];
			if (s1 == s2)
				if (sum - s1 - s2 == s2) cnt++;
		}
	}

	cout << cnt << endl;

	return 0;
}
/*

*/


// In the Name of Allah
// AD13
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef unsigned long long  ull;
typedef pair <int, int> pii;
const double eps = 1e-14; // 15 is also used!
const int INF = 2147483647;
//#define For(i, a, b) for (int i = (a); i < (b); i++)
#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << err; while(1); }

/*-------------------------------------------------------------------------------------*/
/*_____________________________________________________________________________________*/

ll arr [5 * 100345];
int zero_until [5 * 100345];

int main () {
	ll n, sum;
	sum = 0;
	cin >> n;
	for (int i=0; i<n; i++) {
		cin >> arr[i];
		sum += arr[i];
	}
	for (int i=0; i<n; i++) {
		if (arr[i] == 0) {
			int j = i+1;
			while (j < n && arr[j] == 0) j++;
			for(int k=j-1; k>=i;k--)
				zero_until[k] = j;
			i = j-1;
		}
	}
	for (int i=0; i<n; i++)
		cout << zero_until[i] << ' ';
	cout << endl;

	ll cnt = 0;
	ll s1 = 0;
	for (int i=0; i<n-2; i++) {
		s1 += arr[i];
		if (sum != s1 * 3) continue;
		ll s2 = 0;
		for (int j=i+1; j<n-1; j++){
			s2 += arr[j];
			if (s1 == s2) {
				cnt++;
				if (arr[j] == 0) {
					int k = zero_until[j];
					if (k < n-1) cnt += k-j-1;
					else cnt += k-j-2;
					j = k-1;
				}
			}
			else if (arr[j] == 0){
				j = zero_until[j]-1;
			}
		}
	}

	cout << cnt << endl;

	return 0;
}
/*
9
0 0 0 0 0 0 0 0 0
--> 28
7
3 3 0 0 0 3 0
--> 4
*/


// In the Name of Allah
// AD13
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef unsigned long long  ull;
typedef pair <int, int> pii;
const double eps = 1e-14; // 15 is also used!
const int INF = 2147483647;
//#define For(i, a, b) for (int i = (a); i < (b); i++)
#define debug(x) { cerr << #x << " = _" << (x) << "_" << endl; }
void Error(string err) { cout << err; cerr << err; while(1); }

/*-------------------------------------------------------------------------------------*/
/*_____________________________________________________________________________________*/

ll arr [5 * 100345];
int zero_until [5 * 100345];

int main () {
	ll n, sum;
	sum = 0;
	cin >> n;
	for (int i=0; i<n; i++) {
		cin >> arr[i];
		sum += arr[i];
	}
	for (int i=0; i<n; i++) {
		zero_until[i] = 0;
		if (arr[i] == 0) {
			int j = i+1;
			while (j < n && arr[j] == 0) j++;
			zero_until[i] = j;
			i = j-1;
		}
	}

	ll cnt = 0;
	ll s1 = 0;
	for (int i=0; i<n-2; i++) {
		s1 += arr[i];
		if (sum != s1 * 3) continue;
		ll s2 = 0;
		for (int j=i+1; j<n-1; j++){
			s2 += arr[j];
			if (s1 == s2) {
				cnt++;
				if (arr[j] == 0) {
					int k = zero_until[j];
					cnt += k-j-1;
					j = k-1;
				}
			}
			else if (arr[j] == 0){
				j = zero_until[j]-1;
			}
		}
	}

	cout << cnt << endl;

	return 0;
}
/*
6
3 3 0 0 0 3
--> 4
*/


#include<iostream>
#define N 500005
using namespace std;
int sum[N], number[N], fenlie[N];
int main()
{
	int i, n, sumn=0, k=0;
	//int *sum = new int[N];
	//int *number = new int[N];
	//int *fenlie = new int[N];
	memset(sum, 0, sizeof(sum));
	cin >> n;
	for (i = 1;i <= n;i++)
	{
		cin >> number[i];
		sum[i] += sum[i - 1] + number[i];
	}
	if (sum[n] % 3 != 0)
		cout << '0' << endl;
	else
	{
		int temp = sum[n] / 3;
		int j = 0;
		for (i = 1;i <= n;i++)
		{
			if (sum[i] == temp || sum[i] == 2 * temp)
			{
				fenlie[j] = sum[i];
				j++;
			}
		}
		if (temp == 0)
			sumn = (j - 1)*(j - 2) / 2;
		else
		{
			for (i = 0;i < j;i++)
			{
				if (fenlie[i] == temp)
						k++;
				else if (fenlie[i] == 2 * temp)
					sumn += k;
			}
			}
		cout << sumn << endl;
	}
	/*delete sum;
	delete fenlie;
	delete number;*/
	return 0;
}


#include<iostream>
#define N 50005
using namespace std;
int main()
{
	int i, n, sumn=0, k=1;
	int sum[N], number[N], fenlie[N];
	//int *sum = new int[N];
	//int *number = new int[N];
	//int *fenlie = new int[N];
	memset(sum, 0, sizeof(sum));
	cin >> n;
	for (i = 1;i <= n;i++)
	{
		cin >> number[i];
		sum[i] += sum[i - 1] + number[i];
	}
	if (sum[n] % 3 != 0)
		cout << '0' << endl;
	else
	{
		int temp = sum[n] / 3;
		int j = 0;
		for (i = 1;i <= n;i++)
		{
			if (sum[i] == temp || sum[i] == 2 * temp)
			{
				fenlie[j] = sum[i];
				j++;
			}
		}
		if (temp == 0)
			sumn = (j - 1)*(j - 2) / 2;
		else
		{
			for (i = 1;i < j;i++)
			{
				if (fenlie[i] == temp)
						k++;
				else if (fenlie[i] == 2 * temp)
					sumn += k;
			}
			}
		cout << sumn << endl;
	}
	/*delete sum;
	delete fenlie;
	delete number;*/
	return 0;
}


#include<iostream>
#define N 500005
using namespace std;
int main()
{
	int i, n;
	int *sum = new int[N];
	int *number = new int[N];
	memset(sum, 0, sizeof(sum));
	cin >> n;
	for (i = 1;i <= n;i++)
	{
		cin >> number[i];
		sum[i] += sum[i - 1] + number[i];
	}
	if (sum[n] % 3 != 0)
		cout << '0' << endl;
	else
	{
		for(i=1;i<=n;i++)
			if (sum[i] == sum[n] / 3)
			{
				cout << i << endl;
				break;
			}
	}
	return 0;
}


#include<iostream>
#define N 500005
using namespace std;
long long sum[N];
long long number[N];
long long fenlie[N];
int main()
{
	long long i, n, k=0;
	long long sumn = 0;
	//int *sum = new int[N];
	//int *number = new int[N];
	//int *fenlie = new int[N];
	sum[0] = 0;
	cin >> n;
	for (i = 1;i <= n;i++)
	{
		cin >> number[i];
		sum[i] += sum[i - 1] + number[i];
	}
	if (sum[n] % 3 != 0)
		cout << '0' << endl;
	else
	{
		long long temp = sum[n] / 3;
		long long  j = 0;
		for (i = 1;i <= n;i++)
		{
			if (sum[i] == temp || sum[i] == 2 * temp)
			{
				fenlie[j] = sum[i];
				j++;
			}
		}
		if (temp == 0)
			sumn = (j - 1)*(j - 2) / 2;
		else
		{
			for (i = 0;i < j;i++)
			{
				if (fenlie[i] == temp)
						k++;
				else if (fenlie[i] == 2 * temp)
					sumn += k;
			}
			}
		cout << sumn << endl;
	}
	/*delete sum;
	delete fenlie;
	delete number;*/
	return 0;
}


#include<iostream>
#define N 500005
using namespace std;
int main()
{
	int i, n, sumn=0, k=0;
	int sum[N], number[N], fenlie[N];
	//int *sum = new int[N];
	//int *number = new int[N];
	//int *fenlie = new int[N];
	memset(sum, 0, sizeof(sum));
	cin >> n;
	for (i = 1;i <= n;i++)
	{
		cin >> number[i];
		sum[i] += sum[i - 1] + number[i];
	}
	if (sum[n] % 3 != 0)
		cout << '0' << endl;
	else
	{
		int temp = sum[n] / 3;
		int j = 0;
		for (i = 1;i <= n;i++)
		{
			if (sum[i] == temp || sum[i] == 2 * temp)
			{
				fenlie[j] = sum[i];
				j++;
			}
		}
		if (temp == 0)
			sumn = (j - 1)*(j - 2) / 2;
		else
		{
			for (i = 0;i < j;i++)
			{
				if (fenlie[i] == temp)
						k++;
				else if (fenlie[i] == 2 * temp)
					sumn += k;
			}
			}
		cout << sumn << endl;
	}
	/*delete sum;
	delete fenlie;
	delete number;*/
	return 0;
}


#include<iostream>
#define N 500005
using namespace std;
long long sum[N];
long long number[N];
long long fenlie[N];
int main()
{
	int i, n, sumn=0, k=0;
	//int *sum = new int[N];
	//int *number = new int[N];
	//int *fenlie = new int[N];
	sum[0] = 0;
	cin >> n;
	for (i = 1;i <= n;i++)
	{
		cin >> number[i];
		sum[i] += sum[i - 1] + number[i];
	}
	if (sum[n] % 3 != 0)
		cout << '0' << endl;
	else
	{
		int temp = sum[n] / 3;
		int j = 0;
		for (i = 1;i <= n;i++)
		{
			if (sum[i] == temp || sum[i] == 2 * temp)
			{
				fenlie[j] = sum[i];
				j++;
			}
		}
		if (temp == 0)
			sumn = (j - 1)*(j - 2) / 2;
		else
		{
			for (i = 0;i < j;i++)
			{
				if (fenlie[i] == temp)
						k++;
				else if (fenlie[i] == 2 * temp)
					sumn += k;
			}
			}
		cout << sumn << endl;
	}
	/*delete sum;
	delete fenlie;
	delete number;*/
	return 0;
}


#include <cstdio>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <iostream>
#include <utility>
#include <time.h>

//#define DEBUG
#define IN_FILE "input.txt"
#define OUT_FILE "output.txt"

#define mp make_pair
#define ALP 26

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const int MAXN = 500500;

ll a[MAXN];
ll sum[MAXN];

int main()
{
#ifdef DEBUG
	freopen(IN_FILE, "r", stdin);
	freopen(OUT_FILE, "w", stdout);
#endif

	ll n, k;

	cin >> n;
	cin >> a[0];
	sum[0] = a[0];

	vector < int > ind3, ind6, ind9;

	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	if ( sum[n - 1] % 3 != 0 || n < 3 )
	{
		printf("0");
		return 0;
	}

	k = sum[n - 1] / 3;

	ll ans = 0;

	for ( int i = 0; i < n - 1; i++)
	{
		if ( sum[i] == 1*k )
			ind3.push_back(i);
		if ( sum[i] == 2*k )
			ind6.push_back(i);
	}
	ind9.push_back( n - 1 );
	int it1 = 0, it2 = 0, it3 = 0, sz3 = ind3.size(), sz6 = ind6.size();
	for ( it1 = 0; it1 < sz3; it1++)
	{
		while( ind6[it2] <= ind3[it1] )
		{
			it2++;
			if (it2 == sz6)
				break;
		}
		if ( it2 == sz6 )
			break;
		if (ind6[it2] != ind9[0])
		{
			ans += (sz6 - it2);
		}
	}
	printf("%d", ans);
	return 0;
}


#include <cstdio>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <iostream>
#include <utility>
#include <time.h>

//#define DEBUG
#define IN_FILE "input.txt"
#define OUT_FILE "output.txt"

#define mp make_pair
#define ALP 26

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const int MAXN = 500500;

ll a[MAXN];
ll sum[MAXN];

int main()
{
#ifdef DEBUG
	freopen(IN_FILE, "r", stdin);
	freopen(OUT_FILE, "w", stdout);
#endif

	ll n, k;

	cin >> n;
	cin >> a[0];
	sum[0] = a[0];

	vector < int > ind3, ind6, ind9;

	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	if ( sum[n - 1] % 3 != 0 || n < 3 )
	{
		printf("0");
		return 0;
	}

	k = sum[n - 1] / 3;

	ll ans = 0;

	for ( int i = 0; i < n - 1; i++)
	{
		if ( sum[i] == 1*k )
			ind3.push_back(i);
		if ( sum[i] == 2*k )
			ind6.push_back(i);
	}
	ind9.push_back( n - 1 );
	int it1 = 0, it2 = 0, it3 = 0, sz3 = ind3.size(), sz6 = ind6.size();
	for ( it1 = 0; it1 < sz3; it1++)
	{
		while( ind6[it2] <= ind3[it1] )
		{
			it2++;
			if (it2 == sz6)
				break;
		}
		ans += (sz6 - it2);
	}
	printf("%d", ans);
	return 0;
}


#include <cstdio>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <iostream>
#include <utility>
#include <time.h>

//#define DEBUG
#define IN_FILE "input.txt"
#define OUT_FILE "output.txt"

#define mp make_pair
#define ALP 26

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const int MAXN = 500500;

ll a[MAXN];
ll sum[MAXN];

int main()
{
#ifdef DEBUG
	freopen(IN_FILE, "r", stdin);
	freopen(OUT_FILE, "w", stdout);
#endif

	ll n, k;

	cin >> n;
	cin >> a[0];
	sum[0] = a[0];

	vector < int > ind3, ind6, ind9;

	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	if ( sum[n - 1] % 3 != 0 || n < 3 )
	{
		printf("0");
		return 0;
	}

	k = sum[n - 1] / 3;

	ll ans = 0;

	for ( int i = 0; i < n; i++)
	{
		if ( sum[i] == 1*k )
			ind3.push_back(i);
		if ( sum[i] == 2*k )
			ind6.push_back(i);
	}
	ind9.push_back( n - 1 );
	int it1 = 0, it2 = 0, it3 = 0, sz6 = ind6.size(), sz9 = ind9.size();
	for ( it1 = 0; it1 < ind3.size(); it1++)
	{
		while( ind6[it2] <= ind3[it1] )
		{
			it2++;
			if (it2 == sz6)
				break;
		}
		for ( int i = it2; i < sz6; i++)
		{
			int j = 0;
			while (ind9[j] <= ind6[i])
			{
				j++;
				if ( j == sz9 )
					break;
			}
			ans += (sz9 - j);
		}
	}
	printf("%d", ans);
	return 0;
}


#include <cstdio>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <iostream>
#include <utility>
#include <time.h>

//#define DEBUG
#define IN_FILE "input.txt"
#define OUT_FILE "output.txt"

#define mp make_pair
#define ALP 26

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const int MAXN = 500500;

ll a[MAXN];
ll sum[MAXN];

int main()
{
#ifdef DEBUG
	freopen(IN_FILE, "r", stdin);
	freopen(OUT_FILE, "w", stdout);
#endif

	ll n, k;

	cin >> n;
	cin >> a[0];
	sum[0] = a[0];

	vector < int > ind3, ind6, ind9;

	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	if ( sum[n - 1] % 3 != 0 || n < 3 )
	{
		printf("0");
		return 0;
	}

	k = sum[n - 1] / 3;

	ll ans = 0;

	for ( int i = 0; i < n; i++)
	{
		if ( sum[i] == 1*k )
			ind3.push_back(i);
		if ( sum[i] == 2*k )
			ind6.push_back(i);
	}
	ind9.push_back( n - 1 );
	int it1 = 0, it2 = 0, it3 = 0, sz6 = ind6.size(), sz9 = ind9.size();
	for ( it1 = 0; it1 < ind3.size(); it1++)
	{
		while( ind6[it2] <= ind3[it1] )
		{
			it2++;
			if (it2 == sz6)
				break;
		}
		if ( it2 == sz6 )
			break;
		if ( ind6[it2] != ind9[0] && k != 0)
			ans += (sz6 - it2); else
			ans += (sz6 - it2 - 1);
	}
	printf("%d", ans);
	return 0;
}


#include <cstdio>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <iostream>
#include <utility>
#include <time.h>

#define IN_FILE "input.txt"
#define OUT_FILE "output.txt"

#define mp make_pair
#define ALP 26

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const int MAXN = 500500;

ll a[MAXN];
ll sum[MAXN];

int main()
{
#ifdef DEBUG
	freopen(IN_FILE, "r", stdin);
	freopen(OUT_FILE, "w", stdout);
#endif

	ll n, k;

	cin >> n;
	cin >> a[0];
	sum[0] = a[0];

	vector < int > ind3, ind6, ind9;

	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	if ( sum[n - 1] % 3 != 0 || n < 3 )
	{
		printf("0");
		return 0;
	}

	k = sum[n - 1] / 3;

		ll ans = 0;

		for ( int i = 0; i < n; i++)
		{
			if ( sum[i] == 1*k )
				ind3.push_back(i);
			if ( sum[i] == 2*k )
				ind6.push_back(i);
			if ( sum[i] == 3*k )
				ind9.push_back(i);
		}

		int it1 = 0, it2 = 0, it3 = 0, sz6 = ind6.size(), sz9 = ind9.size();
		for ( it1 = 0; it1 < ind3.size(); it1++)
		{
			while( ind6[it2] <= ind3[it1] )
			{
				it2++;
				if (it2 == sz6)
					break;
			}
			for ( int i = it2; i < ind6.size(); i++)
			{
				int j = 0;
				while (ind9[j] <= ind6[i])
				{
					j++;
					if ( j == sz9)
						break;
				}
				ans += (sz9 - j);
			}
		}
		printf("%d", ans);
	return 0;
}


#include <cstdio>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <iostream>
#include <utility>
#include <time.h>

//#define DEBUG
#define IN_FILE "input.txt"
#define OUT_FILE "output.txt"

#define mp make_pair
#define ALP 26

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const int MAXN = 500500;

ll a[MAXN];
ll sum[MAXN];

int main()
{
#ifdef DEBUG
	freopen(IN_FILE, "r", stdin);
	freopen(OUT_FILE, "w", stdout);
#endif

	ll n, k;

	cin >> n;
	cin >> a[0];
	sum[0] = a[0];

	vector < int > ind3, ind6;

	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	if ( sum[n - 1] % 3 != 0 || n < 3 )
	{
		printf("0");
		return 0;
	}

	k = sum[n - 1] / 3;

	ll ans = 0;

	for ( int i = 0; i < n - 1; i++)
	{
		if ( sum[i] == 1*k )
			ind3.push_back(i);
		if ( sum[i] == 2*k )
			ind6.push_back(i);
	}
	int it1 = 0, it2 = 0, sz3 = ind3.size(), sz6 = ind6.size();
	for ( it1 = 0; it1 < sz3; it1++)
	{
		while( ind6[it2] <= ind3[it1] )
		{
			it2++;
			if (it2 == sz6)
				break;
		}
		ans += (sz6 - it2);
	}
	printf("%I64d", ans);
	return 0;
}


#include <cstdio>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <iostream>
#include <utility>
#include <time.h>

//#define DEBUG
#define IN_FILE "input.txt"
#define OUT_FILE "output.txt"

#define mp make_pair
#define ALP 26

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const int MAXN = 500500;

ll a[MAXN];
ll sum[MAXN];

int main()
{
#ifdef DEBUG
	freopen(IN_FILE, "r", stdin);
	freopen(OUT_FILE, "w", stdout);
#endif

	ll n, k;

	cin >> n;
	cin >> a[0];
	sum[0] = a[0];

	vector < int > ind3, ind6, ind9;

	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	if ( sum[n - 1] % 3 != 0 || n < 3 )
	{
		printf("0");
		return 0;
	}

	k = sum[n - 1] / 3;

	ll ans = 0;

	for ( int i = 0; i < n; i++)
	{
		if ( sum[i] == 1*k )
			ind3.push_back(i);
		if ( sum[i] == 2*k )
			ind6.push_back(i);
	}
	ind9.push_back( n - 1 );
	int it1 = 0, it2 = 0, it3 = 0, sz6 = ind6.size(), sz9 = ind9.size();
	for ( it1 = 0; it1 < ind3.size(); it1++)
	{
		while( ind6[it2] <= ind3[it1] )
		{
			it2++;
			if (it2 == sz6)
				break;
		}
		if ( it2 == sz6 )
			break;
		if (ind6[it2] != ind9[0])
		{
			if ( k != 0 )
				ans += (sz6 - it2); else
				ans += (sz6 - it2 - 1);
		}
	}
	printf("%d", ans);
	return 0;
}


#include <cstdio>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <iostream>
#include <utility>
#include <time.h>

//#define DEBUG
#define IN_FILE "input.txt"
#define OUT_FILE "output.txt"

#define mp make_pair
#define ALP 26

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;

const int MAXN = 500500;

ll a[MAXN];
ll sum[MAXN];

int main()
{
#ifdef DEBUG
	freopen(IN_FILE, "r", stdin);
	freopen(OUT_FILE, "w", stdout);
#endif

	ll n, k;

	cin >> n;
	cin >> a[0];
	sum[0] = a[0];

	vector < int > ind3, ind6, ind9;

	for (int i = 1; i < n; i++)
	{
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
	}
	if ( sum[n - 1] % 3 != 0 || n < 3 )
	{
		printf("0");
		return 0;
	}

	k = sum[n - 1] / 3;

	ll ans = 0;

	for ( int i = 0; i < n; i++)
	{
		if ( sum[i] == 1*k )
			ind3.push_back(i);
		if ( sum[i] == 2*k )
			ind6.push_back(i);
	}
	ind9.push_back( n - 1 );
	int it1 = 0, it2 = 0, it3 = 0, sz6 = ind6.size(), sz9 = ind9.size();
	for ( it1 = 0; it1 < ind3.size(); it1++)
	{
		while( ind6[it2] <= ind3[it1] )
		{
			it2++;
			if (it2 == sz6)
				break;
		}
		if ( it2 == sz6 )
			break;
		if ( ind6[it2] != ind9[0] )
			ans+= (sz6 - it2 - 1);
	}
	printf("%d", ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<iomanip>
using namespace std;
int a[500001];
int main()
{
	int total;
	long long sum = 0;
	cin >> total;
	for (int m = 1; m <= total;m++)
	{
		cin >> a[m]; sum += a[m];
	}
	if (sum%3)
	{
		cout << 0; return 0;
	}
	int tar = sum/ 3,q=0,w=0,i=1;
	for (int cur = 0; i <= total;i++)
	{
		q = 1;
		cur += a[i];
		if (cur==tar)
		{
			i++;
			for (; i < total-1&&a[i]==0; i++)
				q++;
			break;
		}
	}
	for (int cur = 0; i <= total; i++)
	{
		cur += a[i];
		if (cur == tar)
		{
			w = 1; i++;
			for (; i < total&&a[i] == 0; i++)
				w++;
			break;
		}
	}
	cout << w*q;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<iomanip>
using namespace std;
int a[500001];
int main()
{
	int total;
	long long sum = 0;
	cin >> total;
	for (int m = 1; m <= total;m++)
	{
		cin >> a[m]; sum += a[m];
	}
	if (sum%3)
	{
		cout << 0; return 0;
	}
	int tar = sum/ 3,q=0,w=0,i=1;
	for (int cur = 0; i <= total;i++)
	{
		q = 1;
		cur += a[i];
		if (cur==tar)
		{
			i++;
			for (; i < total-1&&a[i]==0; i++)
				q++;
			break;
		}
	}
	for (int cur = 0; i < total; i++)
	{
		cur += a[i];
		if (cur == tar)
		{
			w = 1; i++;
			for (; i < total&&a[i] == 0; i++)
				w++;
			break;
		}
	}
	cout << w*q;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<iomanip>
using namespace std;
int a[500001];
map<int, int>t1,t2;
int main()
{
	int total;
	long long sum = 0;
	cin >> total;
	for (int m = 1; m <= total;m++)
	{
		cin >> a[m]; sum += a[m];
	}
	if (sum%3)
	{
		cout << 0; return 0;
	}
	long long tar = sum/ 3,q=0,w=0,i=1;
	for (long long cur = 0; i <= total;i++)
	{
		cur += a[i];
		if (cur==tar)

			t1[i] =0;
	}
	i = total;
	for (long long cur = 0,all=1; i>0; i--)
	{
		cur += a[i];
		if (cur == tar)
		{
			t2[i] = all++;
		}
	}
	long long ans = 0;
	for (map<int, int>::iterator it = t1.begin(); it != t1.end();it++)
	{
		map<int, int>::iterator itt = t2.lower_bound(it->first+2);
		if (itt != t2.end())
		ans += itt->second;
	}
	cout << ans;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<iomanip>
using namespace std;
int a[500001];
map<int, int>t1,t2;
int main()
{
	int total;
	long long sum = 0;
	cin >> total;
	for (int m = 1; m <= total;m++)
	{
		cin >> a[m]; sum += a[m];
	}
	if (sum%3)
	{
		cout << 0; return 0;
	}
	long long tar = sum/ 3,q=0,w=0,i=1;
	for (long long cur = 0; i <= total;i++)
	{
		cur += a[i];
		if (cur==tar)

			t1[i] =0;
	}
	i = total;
	for (long long cur = 0,all=1; i>0; i--)
	{
		cur += a[i];
		if (cur == tar)
		{
			t2[i] = all++;
		}
	}
	int ans = 0;
	for (map<int, int>::iterator it = t1.begin(); it != t1.end();it++)
	{
		map<int, int>::iterator itt = t2.lower_bound(it->first+2);
		if (itt != t2.end())
		ans += itt->second;
	}
	cout << ans;
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int integer;
	int* it;
	int sum=0;
	int n;
	cin >> n;
	it = new int [n];
	int check0 = 0;
	for (int i=0; i<n; i++ )
	{
		cin >> integer;

		it[i] = integer;
		if (integer ==0)
		{
		    check0+=1;
		}
	}
	int count =0;

	if ( check0 ==n)
	{
	    int plus =2;
	    while (plus<n)
	    {
	        count+=n-plus;
	    }
	}
	else
	{
	// h : number of element
	for ( int h=1; h<n-2; h++)
	{
		int sum1 =0, j;
		for ( j=0; j<h; j++ )
		{
			if ( it[j] == 0)
			{
				count+=1;
			}
			sum1 += it[j];
		}
		int sum2 = it[j], k = 0, check =0;
		if ( it[j] < sum1 )
		{
			for ( k=j+1; sum2 <= sum1; k ++ )
			{
				if ( sum2 == sum1 )
				{
					check =1;
					break;
				}
				sum2 += it[k];
			}
		}
		int l, sum3 = 0;
		if (( sum2 == sum1 ) && ( check == 1))
		{
			for ( l = k+1; l<n; l++ )
			{
				if ( it[l] == 0)
			    {
				count+=1;
			    }
				sum3+=it[l];
			}
		}
		else if (( sum2 == sum1 ) && ( check == 0))
		{
			for ( l = j+1; l<n; l++ )
			{
				if ( it[l] == 0 )
				{
					count+=1;
				}
				sum3+=it[l];
			}
		}
		if (( sum1 == sum2 ) && ( sum2 == sum3 ))
		{
			count +=1;
		}
	}
	}
	cout << count << endl;
	delete [n] it;
}


#include <iostream>
using namespace std;

int main()
{
	int integer;
	int* it;
	int sum=0;
	int n;
	cin >> n;
	it = new int [n];
	int check0 = 0;
	for (int i=0; i<n; i++ )
	{
		cin >> integer;

		it[i] = integer;
		if (integer ==0)
		{
		    check0+=1;
		}
	}
	int count =0;

	if ( check0 ==n)
	{
	    int plus =2;
	    while (plus<n)
	    {
	        count+=n-plus;
	    }
	}
	// h : number of element
	for ( int h=1; h<n-2; h++)
	{
		int sum1 =0, j;
		for ( j=0; j<h; j++ )
		{
			if ( it[j] == 0)
			{
				count+=1;
			}
			sum1 += it[j];
		}
		int sum2 = it[j], k = 0, check =0;
		if ( it[j] < sum1 )
		{
			for ( k=j+1; sum2 <= sum1; k ++ )
			{
				if ( sum2 == sum1 )
				{
					check =1;
					break;
				}
				sum2 += it[k];
			}
		}
		int l, sum3 = 0;
		if (( sum2 == sum1 ) && ( check == 1))
		{
			for ( l = k+1; l<n; l++ )
			{
				if ( it[l] == 0)
			    {
				count+=1;
			    }
				sum3+=it[l];
			}
		}
		else if (( sum2 == sum1 ) && ( check == 0))
		{
			for ( l = j+1; l<n; l++ )
			{
				if ( it[l] == 0 )
				{
					count+=1;
				}
				sum3+=it[l];
			}
		}
		if (( sum1 == sum2 ) && ( sum2 == sum3 ))
		{
			count +=1;
		}
	}
	cout << count << endl;
	delete [n] it;
}


#include <iostream>
using namespace std;

int main()
{
int n,s=0,a,count=0;
long long b[500004]
    cin>>n;
    for (int i=0; i<n; i++)
    {
        cin >> a;
        s+=a;
        b[i] = s;
    }
    a=0;
    for (int i=0; i<n-1; i++)
    {
        if ((i>0) && (i<n-1) && (b[i]*3==b[n-1]*2))
        {
            count+=a;
        }
        if (b[i]*3 == b[n-1])
        {
            a++;
        }
    }
    cout << count;
}


#include <iostream>
using namespace std;

int main()
{
	int integer,s=0;
	int* it;
	int sum=0;
	int n;
	cin >> n;
	it = new int [n];
	for (int i=0; i<n; i++ )
	{
		cin >> integer;
		s+=integer;
		it[i] = s;

	}
	int count =0, a=0;
	 for (int i=0; i<n-1; i++)
    {
        if ((i>0) && (i<n-1) && (it[i]*3==it[n-1]*2))
        {
            count+=a;
        }
        if (it[i]*3 == it[n-1])
        {
            a++;
        }
    }
	cout << count << endl;
	delete [n] it;
}


#include <iostream>
using namespace td;

int main()
{
    int n,s=0,a,b[100000],count;
    cin>>n;
    for (int i=0; i<n; i++)
    {
        cin >> a;
        s+=a;
        b[i] = s;
    }
    a=0;
    for (int i=0; i<n-1; i++)
    {
        if (i>0 && i<n-1 && b[i]*3==b[n-1]*2)
        {
            count+=a;
        }
        if (b[i]*3 == b[n-1])
        {
            a++;
        }
    }
    cout << count;
}


#include <iostream>
using namespace std;

int main()
{
	int integer;
	int* it;
	int sum=0;
	int n;
	cin >> n;
	it = new int [n];
	for (int i=0; i<n; i++ )
	{
		cin >> integer;
		it[i] = integer;
	}
	int count =0;
	// h : number of element
	for ( int h=1; h<n-2; h++)
	{
		int sum1 =0, j;
		for ( j=0; j<h; j++ )
		{
			if ( it[j] == 0)
			{
				count+=1;
			}
			sum1 += it[j];
		}
		int sum2 = it[j], k = 0, check =0;
		if ( it[j] < sum1 )
		{
			for ( k=j+1; sum2 <= sum1; k ++ )
			{
				if ( sum2 == sum1 )
				{
					check =1;
					break;
				}
				sum2 += it[k];
			}
		}
		int l, sum3 = 0;
		if (( sum2 == sum1 ) && ( check == 1))
		{
			for ( l = k+1; l<n; l++ )
			{
				if ( it[l] == 0)
			    {
				count+=1;
			    }
				sum3+=it[l];
			}
		}
		else if (( sum2 == sum1 ) && ( check == 0))
		{
			for ( l = j+1; l<n; l++ )
			{
				if ( it[l] == 0 )
				{
					count+=1;
				}
				sum3+=it[l];
			}
		}
		if (( sum1 == sum2 ) && ( sum2 == sum3 ))
		{
			count +=1;
		}
	}
	cout << count << endl;
	delete [n] it;
}


#include <iostream>
using namespace std;

int main()
{
	int integer;
	int* it;
	int sum=0;
	int n;
	cin >> n;
	it = new int [n];
	int check0 = 0;
	for (int i=0; i<n; i++ )
	{
		cin >> integer;

		it[i] = integer;
		if (integer ==0)
		{
		    check0+=1;
		}
	}
	int count =0;

	if ( check0 ==n)
	{
	    int plus =2;
	    while (plus<n)
	    {
	        count+=n-plus;
	        plus+=1;
	    }
	}
	else
	{
	// h : number of element
	for ( int h=1; h<n-2; h++)
	{
		int sum1 =0, j;
		for ( j=0; j<h; j++ )
		{
			if ( it[j] == 0)
			{
				count+=1;
			}
			sum1 += it[j];
		}
		int sum2 = it[j], k = 0, check =0;
		if ( it[j] < sum1 )
		{
			for ( k=j+1; sum2 <= sum1; k ++ )
			{
				if ( sum2 == sum1 )
				{
					check =1;
					break;
				}
				sum2 += it[k];
			}
		}
		int l, sum3 = 0;
		if (( sum2 == sum1 ) && ( check == 1))
		{
			for ( l = k+1; l<n; l++ )
			{
				if ( it[l] == 0)
			    {
				count+=1;
			    }
				sum3+=it[l];
			}
		}
		else if (( sum2 == sum1 ) && ( check == 0))
		{
			for ( l = j+1; l<n; l++ )
			{
				if ( it[l] == 0 )
				{
					count+=1;
				}
				sum3+=it[l];
			}
		}
		if (( sum1 == sum2 ) && ( sum2 == sum3 ))
		{
			count +=1;
		}
	}
	}
	cout << count << endl;
	delete [n] it;
}


#include <iostream>
using namespace std;

int main()
{
	int integer;
	int* it;
	int sum=0;
	int n;
	cout << "How many integer: " << endl;
	cin >> n;
	it = new int [n];
	for (int i=0; i<n; i++ )
	{
		cin >> integer;
		it[i] = integer;
	}
	int count =0;
	// h : number of element
	for ( int h=1; h<n-2; h++)
	{
		int sum1 =0, j;
		for ( j=0; j<h; j++ )
		{
			if ( it[j] == 0)
			{
				count+=1;
			}
			sum1 += it[j];
		}
		int sum2 = it[j], k = 0, check =0;
		if ( it[j] < sum1 )
		{
			for ( k=j+1; sum2 <= sum1; k ++ )
			{
				if ( sum2 == sum1 )
				{
					check =1;
					break;
				}
				sum2 += it[k];
			}
		}
		int l, sum3 = 0;
		if (( sum2 == sum1 ) && ( check == 1))
		{
			for ( l = k+1; l<n; l++ )
			{
				if ( it[l] == 0)
			    {
				count+=1;
			    }
				sum3+=it[l];
			}
		}
		else if (( sum2 == sum1 ) && ( check == 0))
		{
			for ( l = j+1; l<n; l++ )
			{
				if ( it[l] == 0 )
				{
					count+=1;
				}
				sum3+=it[l];
			}
		}
		if (( sum1 == sum2 ) && ( sum2 == sum3 ))
		{
			count +=1;
		}
	}
	cout << "Ways: " << count << endl;
	delete [n] it;
}


#include <iostream>
using namespace std;
int n, i, a;
long long b[500000], s, ans;
int main()
{
	cin>>n;
	for (i=0; i<n; i++) {
		cin>>a;
		s+=a; b[i]=s;
	}
	for (a=0,i=0; i<n; i++) {
		if (i>0 && i<n-1 && b[i]*3==b[n-1]*2) ans+=a;
		if (b[i]*3==b[n-1]) a++;
	}
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <stdio.h>      /* printf */
#include <math.h>
using namespace std;
long long b[500009];
int main(){
   long long n,s(0),am1(0),am2(0);
   //long long a[500009];

   cin>>n;
   cin>>b[0];
   for(long long i=1;i<n;i++){
    cin>>s;

    b[i]=b[i-1]+s;

   }
   if(b[n-1]%3 != 0) cout<<0<<endl;
   else {
        s=b[n-1]/3;
        //long long t(0),sum(0);
        //if(b[0] == s)am1++;
        for(long long i=1;i<n-1;i++){
            //cout<<b[i]<<endl;
            if(b[i] == s)am1++;
            if(b[i] == s*2){am2+=am1;
            if(b[0] == s)am2++;
            }
        }

   if(s) cout<<am2<<endl;
   else cout<<(am1-1)*am1/2 +am1<<endl;

   }
   //system("pause");
    return 0;
}


#include <iostream>
#include <stdio.h>      /* printf */
#include <math.h>
using namespace std;
long long b[500009];
int main(){
   long long n,s(0),am1(0),am2(0);
   //long long a[500009];

   cin>>n;
   cin>>b[0];
   for(long long i=1;i<n;i++){
    cin>>s;

    b[i]=b[i-1]+s;

   }
   if(b[n-1]%3 != 0) cout<<0<<endl;
   else {
        s=b[n-1]/3;
        //long long t(0),sum(0);
        if(b[0] == s)am1++;
        for(long long i=1;i<n-1;i++){
            //cout<<b[i]<<endl;
            if(b[i] == s)am1++;
            if(b[i] == s*2)am2+=am1;
        }

   if(s) cout<<am2<<endl;
   else cout<<(am1-1)*am1/2 +am1<<endl;

   }
   //system("pause");
    return 0;
}


#include <iostream>
#include <stdio.h>      /* printf */
#include <math.h>
using namespace std;

int main(){
   long long n,s(0),am1(0),am2(0);
   //long long a[500009];
   long long b[50009];
   cin>>n;
   cin>>b[0];
   for(long long i=1;i<n;i++){
    cin>>s;

    b[i]=b[i-1]+s;

   }
   if(b[n-1]%3 != 0) cout<<0<<endl;
   else {
        s=b[n-1]/3;
        //long long t(0),sum(0);

        for(long long i=1;i<n-1;i++){
            //cout<<b[i]<<endl;
            if(b[i] == s)am1++;
            if(b[i] == s*2)am2++;
        }

    cout<<am1*am2<<endl;

   }
   //system("pause");
    return 0;
}


#include <iostream>
#include <stdio.h>      /* printf */
#include <math.h>
using namespace std;
long long b[500009];
int main(){
   long long n,s(0),am1(0),am2(0);
   //long long a[500009];

   cin>>n;
   cin>>b[0];
   for(long long i=1;i<n;i++){
    cin>>s;

    b[i]=b[i-1]+s;

   }
   if(b[n-1]%3 != 0) cout<<0<<endl;
   else {
        s=b[n-1]/3;
        //long long t(0),sum(0);
        if(b[0] == s)am2++;
        for(long long i=1;i<n-1;i++){
            //cout<<b[i]<<endl;
            if(b[i] == s)am1++;
            if(b[i] == s*2)am2+=am1;
        }

   if(s) cout<<am2<<endl;
   else cout<<(am1-1)*am1/2 +am1<<endl;

   }
   //system("pause");
    return 0;
}


#include <iostream>
#include <stdio.h>      /* printf */
#include <math.h>
using namespace std;
long long b[500009];
long long a[500009];
int main(){
   long long n,s(0),am1(0),am2(0);
   //long long a[500009];

   cin>>n;
   cin>>b[0];
   for(long long i=1;i<n;i++){
    cin>>a[i];

    b[i]=b[i-1]+a[i];

   }
   if(b[n-1]%3 != 0) cout<<0<<endl;
   else {
        s=b[n-1]/3;
        //long long t(0),sum(0);
        //if(b[0] == s)am1++;

        if(s){
                for(long long i=1;i<n-1;i++){
                    //cout<<b[i]<<endl;
                    if(b[i] == s)am1++;
                    if(b[i] == s*2){am2+=am1;
                    if(b[0] == s)am2++;
                    }
                }
        } else{
            for(long long i=1;i<n-1;i++){

                    if(b[i] == 0 && (a[i]==0 || i== n-2)){am1++;

                    }
                }
        }

   if(s) cout<<am2<<endl;
   else cout<<(am1-1)*am1/2 +am1<<endl;

   }
   //system("pause");
    return 0;
}


#include <iostream>
#include <stdio.h>      /* printf */
#include <math.h>
using namespace std;
long long b[500009];
int main(){
   long long n,s(0),am1(0),am2(0);
   //long long a[500009];

   cin>>n;
   cin>>b[0];
   for(long long i=1;i<n;i++){
    cin>>s;

    b[i]=b[i-1]+s;

   }
   if(b[n-1]%3 != 0) cout<<0<<endl;
   else {
        s=b[n-1]/3;
        //long long t(0),sum(0);

        for(long long i=1;i<n-1;i++){
            //cout<<b[i]<<endl;
            if(b[i] == s)am1++;
            if(b[i] == s*2)am2++;
        }

    cout<<am1*am2<<endl;

   }
   //system("pause");
    return 0;
}


#include <iostream>
#include <stdio.h>      /* printf */
#include <math.h>
using namespace std;
long long b[500009];
int main(){
   long long n,s(0),am1(0),am2(0);
   //long long a[500009];

   cin>>n;
   cin>>b[0];
   for(long long i=1;i<n;i++){
    cin>>s;

    b[i]=b[i-1]+s;

   }
   if(b[n-1]%3 != 0) cout<<0<<endl;
   else {
        s=b[n-1]/3;
        //long long t(0),sum(0);

        for(long long i=1;i<n-1;i++){
            //cout<<b[i]<<endl;
            if(b[i] == s)am1++;
            if(b[i] == s*2)am2+=am1;
        }

   if(s) cout<<am2<<endl;
   else cout<<(am1-1)*am1/2 +am1<<endl;

   }
   //system("pause");
    return 0;
}


#include <iostream>
#include <stdio.h>      /* printf */
#include <math.h>
using namespace std;
long long b[500009];
int main(){
   long long n,s(0),am1(0),am2(0);
   //long long a[500009];

   cin>>n;
   cin>>b[0];
   for(long long i=1;i<n;i++){
    cin>>s;

    b[i]=b[i-1]+s;

   }
   if(b[n-1]%3 != 0) cout<<0<<endl;
   else {
        s=b[n-1]/3;
        //long long t(0),sum(0);

        for(long long i=1;i<n-1;i++){
            //cout<<b[i]<<endl;
            if(b[i] == s)am1++;
            if(b[i] == s*2)am2++;
        }

   if(s) cout<<am1*am2<<endl;
   else cout<<(am1-1)*am1/2 +am1<<endl;

   }
   //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
long long as[500001];
long long cnt1[500001];
int main()
{
    //freopen ("in.txt","r",stdin);
    int n;
    cin >> n;
    long long allsum = 0;
    memset (as,0,sizeof as);
    memset (cnt1,0,sizeof cnt1);
    for (int i = 0; i < n; i++)
    {
        scanf ("%I64d",&as[i]);
        if(i)
            as[i] += as[i - 1];
        allsum = as[i];
    }
    if (abs(allsum) % 3 == 0 && n > 2)
    {
        long long a1 = 0;
        for (int i = 0; i < n - 2; i++)
        {
            if (as[i] == allsum / 3){
                a1++;
                cnt1[i] = a1;

            }
            if (i)
                cnt1[i] += cnt1[i - 1];
        }
        long long a2 = 0;
        long long ans = 0;
        for (int i = n - 1; i >= 2; i--)
        {
            if (allsum - as[i - 1] == allsum / 3)
            {
                a2++;
                if (i - 2 >= 0)
                {
                    ans += cnt1[i - 2];
                }
            }
        }
        cout << ans;
    }
    else
        cout << 0;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
long long as[500001];
long long cnt1[500001];
int main()
{
    //freopen ("in.txt","r",stdin);
    int n;
    cin >> n;
    long long allsum = 0;
    memset (as,0,sizeof as);
    for (int i = 0; i < n; i++)
    {
        scanf ("%I64d",&as[i]);
        if(i)
            as[i] += as[i - 1];
        allsum = as[i];
    }
    if (allsum % 3 == 0 && n > 2)
    {
        int a1 = 0;
        for (int i = 0; i < n - 2; i++)
        {
            if (as[i] == allsum / 3){
                a1++;
                cnt1[i] = a1;
            }
        }
        int a2 = 0;
        for (int i = n - 1; i >= 3; i--)
        {
            if (allsum - as[i - 1] == allsum / 3)
            {
                a2++;
            }
        }
        cout << a1 * 1ll * a2;
    }
    else
        cout << 0;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
long long as[500001];
long long cnt1[500001];
int main()
{
    //freopen ("in.txt","r",stdin);
    int n;
    cin >> n;
    long long allsum = 0;
    memset (as,0,sizeof as);
    for (int i = 0; i < n; i++)
    {
        scanf ("%I64d",&as[i]);
        if(i)
            as[i] += as[i - 1];
        allsum = as[i];
    }
    if (allsum % 3 == 0 && n > 2)
    {
        long long a1 = 0;
        for (int i = 0; i < n - 2; i++)
        {
            if (as[i] == allsum / 3){
                a1++;
                cnt1[i] = a1;

            }
            if (i)
                cnt1[i] += cnt1[i - 1];
        }
        long long a2 = 0;
        long long ans = 0;
        for (int i = n - 1; i >= 2; i--)
        {
            if (allsum - as[i - 1] == allsum / 3)
            {
                a2++;
                if (i - 2 >= 0)
                {
                    ans = max (ans,cnt1[i - 2] * a2);
                }
            }
        }
        cout << ans;
    }
    else
        cout << 0;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
long long as[500001];
long long cnt1[500001];
int main()
{
    //freopen ("in.txt","r",stdin);
    int n;
    cin >> n;
    long long allsum = 0;
    memset (as,0,sizeof as);
    for (int i = 0; i < n; i++)
    {
        scanf ("%I64d",&as[i]);
        if(i)
            as[i] += as[i - 1];
        allsum = as[i];
    }
    if (allsum % 3 == 0 && n > 2)
    {
        long long a1 = 0;
        for (int i = 0; i < n - 2; i++)
        {
            if (as[i] == allsum / 3){
                a1++;
                cnt1[i] = a1;

            }
            if (i)
                cnt1[i] += cnt1[i - 1];
        }
        long long a2 = 0;
        long long ans = 0;
        for (int i = n - 1; i >= 2; i--)
        {
            if (allsum - as[i - 1] == allsum / 3)
            {
                a2++;
                if (i - 2 >= 0)
                {
                    ans += cnt1[i - 2];
                }
            }
        }
        cout << ans;
    }
    else
        cout << 0;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
long long as[500001];
long long cnt1[500001];
int main()
{
    //freopen ("in.txt","r",stdin);
    int n;
    cin >> n;
    long long allsum = 0;
    memset (as,0,sizeof as);
    memset (cnt1,0,sizeof cnt1);
    for (int i = 0; i < n; i++)
    {
        scanf ("%I64d",&as[i]);
        if(i)
            as[i] += as[i - 1];
        allsum = as[i];
    }
    if (abs(allsum) % 3 == 0 && n > 2)
    {
        long long a1 = 0;
        for (int i = 0; i < n - 2; i++)
        {
            if (as[i] == allsum / 3){
                cnt1[i] = 1;

            }
            if (i)
                cnt1[i] += cnt1[i - 1];
        }
        long long a2 = 0;
        long long ans = 0;
        for (int i = n - 1; i >= 2; i--)
        {
            if (allsum - as[i - 1] == allsum / 3)
            {
                a2++;
                if (i - 2 >= 0)
                {
                    ans += cnt1[i - 2];
                }
            }
        }
        cout << ans;
    }
    else
        cout << 0;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
long long as[500001];
long long cnt1[500001];
int main()
{
    //freopen ("in.txt","r",stdin);
    int n;
    cin >> n;
    long long allsum = 0;
    for (int i = 0; i < n; i++)
    {
        scanf ("%d",&as[i]);
        if(i)
            as[i] += as[i - 1];
        allsum = as[i];
    }
    if (allsum % 3 == 0 && n > 2)
    {
        int a1 = 0;
        for (int i = 0; i < n; i++)
        {
            if (as[i] == allsum / 3){
                a1++;
                cnt1[i] = a1;
            }
        }
        int a2 = 0;
        for (int i = n - 1; i >= 1; i--)
        {
            if (allsum - as[i - 1] == allsum / 3)
            {
                a2++;
            }
        }
        cout << a1 * 1ll * a2;
    }
    else
        cout << 0;
    //int ans = 0;
    //for (int i = 0; i < n; i++)
    //{
    //  long long p1 = as[i];
    //  int l = i + 1;
    //  int r = n - 1;
    //  int rs = 0;
    //  while(l <= r)
    //  {
    //      int m = (l + r) / 2;
    //      if (as[m] - p1 <= allsum - as[m])
    //      {
    //          rs = m;
    //          l = m + 1;
    //      }
    //      else
    //          r = m - 1;
    //  }
    //  if (as[rs] - p1 == allsum - as[rs])
    //      ans++;

    //}

    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdarg.h>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<ll> vecll;
typedef vector<db> vecd;
typedef vector<vector<int> > veci2;
typedef vector<pair<int, bool> > vecif;
typedef vector<bool> vecb;
typedef vector<string> vecstr;
typedef vector<pair<int, int> > vecprii;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef map<char,bool> mapcb;
typedef map<char,int> mapci;
typedef map<int, bool> mapib;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
template<class T, class T2> bool cmpfirst(const pair<T, T2> &a, const pair<T, T2> &b) { return a.first > b.first; }
template<class T, class T2> bool cmpsecond(const pair<T, T2> &a, const pair<T, T2> &b) { return a.second > b.second; }
template<class T> inline T abs(T t) {return ((t<0)?(-t):t);}
template<class T> inline T maxv(vector<T> v) { return *(max_element(v.begin(), v.end())); };
template<class T> inline T minv(vector<T> v) { return *(min_element(v.begin(), v.end())); };
template<class T> T gcd(T a, T b) {return ((a == 0)?b:(b == 0)?a:((a > b)?gcd(a - b, b):(a < b)?gcd(a, b - a):a));}
template<class T> T gcd(T a, T b, T c) {return gcd(a, gcd(b, c));}
template<class T> T gcd(T a, T b, T c, T d) {return gcd(gcd(a, d), gcd(b, c));}
template<class T> T lcm(T a, T b) {return (a * b)/gcd(a, b);}
template<class T1, class T2> pair<T1, T2> mp(T1 t1, T2 t2) {return make_pair(t1, t2);}
template<class T> bool in(T a, T min, T max) {return ((a>min && a<max));}
template<class T> bool out(T a, T min, T max) {return ((a<min || a>max));}
template<class T> T sqr(T &a) {return a*a;}
inline bool isUpperCase(char c) {return (c >= 'A' && c <= 'Z');}
inline bool isLowerCase(char c) {return ( c>= 'a' && c <= 'z');}
inline bool isDigit(char c) {return (c >= '0' && c <= '9');}
inline bool isLetter(char c) {return (isUpperCase(c) || isLowerCase(c));}
inline char toLowerCase(char c) {return ((isUpperCase(c))?c+32:c);}
inline char toUpperCase(char c) {return ((isLowerCase(c))?c-32:c);}
//vecull hashString(string s) {vecull res(s.length()); ull m = 17431, t = m; res[0] = s[0]; for (int i = 1; i < s.length(); ++i) res[i] = res[i-1]*t, t*=m; return res;}
//ull subHash(vecull &v, ull l, ull r) {return (v[r] - v[l - 1]);}

#ifdef _MSC_VER
#define typeof(a) decltype(a)
#endif
#define sqr(x) ((x)*(x))
#define fru(i, n) for (int (i) = 0; i < (n); ++i)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define frd(i, n) for (int (i) = n - 1; i >= 0; --i)
#define rdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define rd(name, type) type name; cin >> name;
#define rds(name) string name; getline(cin, name);
#define rd2(name1, type1, name2, type2) rd(name1, type1); rd(name2, type2);
#define rd3(n1, t1, n2, t2, n3, t3) rd2(n1, t1, n2, t2); rd(n3, t3);
#define rd4(n1, t1, n2, t2, n3, t3, n4, t4) rd2(n1, t1, n2, t2); rd2(n3, t3, n4, t4);
#define each(e,s) for(typeof((s).begin()) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define summ(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec, restype) restype name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define F first
#define S second
#define pb push_back

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	rd(n, int);
	vecll a(n);
	vecll f(n);
	cin >> a[0];
	f[0] = a[0];
	for (int i = 1; i < n; ++i)
	{
		cin >> a[i];
		f[i] = f[i-1] + a[i];
	}
	int ans = 0, k = 0;
	for (int i = 1; i < n; i++)
	{
		if (f[i]*3 == f[n-1]*2) ans+=k;
		if (f[i]*3 == f[n-1]) k++;
	}
	cout << ans;
	return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdarg.h>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<ll> vecll;
typedef vector<db> vecd;
typedef vector<vector<int> > veci2;
typedef vector<pair<int, bool> > vecif;
typedef vector<bool> vecb;
typedef vector<string> vecstr;
typedef vector<pair<int, int> > vecprii;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef map<char,bool> mapcb;
typedef map<char,int> mapci;
typedef map<int, bool> mapib;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
template<class T, class T2> bool cmpfirst(const pair<T, T2> &a, const pair<T, T2> &b) { return a.first > b.first; }
template<class T, class T2> bool cmpsecond(const pair<T, T2> &a, const pair<T, T2> &b) { return a.second > b.second; }
template<class T> inline T abs(T t) {return ((t<0)?(-t):t);}
template<class T> inline T maxv(vector<T> v) { return *(max_element(v.begin(), v.end())); };
template<class T> inline T minv(vector<T> v) { return *(min_element(v.begin(), v.end())); };
template<class T> T gcd(T a, T b) {return ((a == 0)?b:(b == 0)?a:((a > b)?gcd(a - b, b):(a < b)?gcd(a, b - a):a));}
template<class T> T gcd(T a, T b, T c) {return gcd(a, gcd(b, c));}
template<class T> T gcd(T a, T b, T c, T d) {return gcd(gcd(a, d), gcd(b, c));}
template<class T> T lcm(T a, T b) {return (a * b)/gcd(a, b);}
template<class T1, class T2> pair<T1, T2> mp(T1 t1, T2 t2) {return make_pair(t1, t2);}
template<class T> bool in(T a, T min, T max) {return ((a>min && a<max));}
template<class T> bool out(T a, T min, T max) {return ((a<min || a>max));}
template<class T> T sqr(T &a) {return a*a;}
inline bool isUpperCase(char c) {return (c >= 'A' && c <= 'Z');}
inline bool isLowerCase(char c) {return ( c>= 'a' && c <= 'z');}
inline bool isDigit(char c) {return (c >= '0' && c <= '9');}
inline bool isLetter(char c) {return (isUpperCase(c) || isLowerCase(c));}
inline char toLowerCase(char c) {return ((isUpperCase(c))?c+32:c);}
inline char toUpperCase(char c) {return ((isLowerCase(c))?c-32:c);}
//vecull hashString(string s) {vecull res(s.length()); ull m = 17431, t = m; res[0] = s[0]; for (int i = 1; i < s.length(); ++i) res[i] = res[i-1]*t, t*=m; return res;}
//ull subHash(vecull &v, ull l, ull r) {return (v[r] - v[l - 1]);}

#ifdef _MSC_VER
#define typeof(a) decltype(a)
#endif
#define sqr(x) ((x)*(x))
#define fru(i, n) for (int (i) = 0; i < (n); ++i)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define frd(i, n) for (int (i) = n - 1; i >= 0; --i)
#define rdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define rd(name, type) type name; cin >> name;
#define rds(name) string name; getline(cin, name);
#define rd2(name1, type1, name2, type2) rd(name1, type1); rd(name2, type2);
#define rd3(n1, t1, n2, t2, n3, t3) rd2(n1, t1, n2, t2); rd(n3, t3);
#define rd4(n1, t1, n2, t2, n3, t3, n4, t4) rd2(n1, t1, n2, t2); rd2(n3, t3, n4, t4);
#define each(e,s) for(typeof((s).begin()) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define summ(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec, restype) restype name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define F first
#define S second
#define pb push_back

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	rd(n, int);
	vecll a(n);
	vecll f(n);
	cin >> a[0];
	f[0] = a[0];
	for (int i = 1; i < n; ++i)
	{
		cin >> a[i];
		f[i] = f[i-1] + a[i];
	}
	ll ans = 0, k = 0;
	for (int i = 1; i < n; i++)
	{
		if (f[i]*3 == f[n-1]*2) ans+=k;
		if (f[i]*3 == f[n-1]) k++;
	}
	cout << ans;
	return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdarg.h>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<ll> vecll;
typedef vector<db> vecd;
typedef vector<vector<int> > veci2;
typedef vector<pair<int, bool> > vecif;
typedef vector<bool> vecb;
typedef vector<string> vecstr;
typedef vector<pair<int, int> > vecprii;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef map<char,bool> mapcb;
typedef map<char,int> mapci;
typedef map<int, bool> mapib;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
template<class T, class T2> bool cmpfirst(const pair<T, T2> &a, const pair<T, T2> &b) { return a.first > b.first; }
template<class T, class T2> bool cmpsecond(const pair<T, T2> &a, const pair<T, T2> &b) { return a.second > b.second; }
template<class T> inline T abs(T t) {return ((t<0)?(-t):t);}
template<class T> inline T maxv(vector<T> v) { return *(max_element(v.begin(), v.end())); };
template<class T> inline T minv(vector<T> v) { return *(min_element(v.begin(), v.end())); };
template<class T> T gcd(T a, T b) {return ((a == 0)?b:(b == 0)?a:((a > b)?gcd(a - b, b):(a < b)?gcd(a, b - a):a));}
template<class T> T gcd(T a, T b, T c) {return gcd(a, gcd(b, c));}
template<class T> T gcd(T a, T b, T c, T d) {return gcd(gcd(a, d), gcd(b, c));}
template<class T> T lcm(T a, T b) {return (a * b)/gcd(a, b);}
template<class T1, class T2> pair<T1, T2> mp(T1 t1, T2 t2) {return make_pair(t1, t2);}
template<class T> bool in(T a, T min, T max) {return ((a>min && a<max));}
template<class T> bool out(T a, T min, T max) {return ((a<min || a>max));}
template<class T> T sqr(T &a) {return a*a;}
inline bool isUpperCase(char c) {return (c >= 'A' && c <= 'Z');}
inline bool isLowerCase(char c) {return ( c>= 'a' && c <= 'z');}
inline bool isDigit(char c) {return (c >= '0' && c <= '9');}
inline bool isLetter(char c) {return (isUpperCase(c) || isLowerCase(c));}
inline char toLowerCase(char c) {return ((isUpperCase(c))?c+32:c);}
inline char toUpperCase(char c) {return ((isLowerCase(c))?c-32:c);}
vecull hashString(string s) {vecull res(s.length()); ull m = 17431, t = m; res[0] = s[0]; for (int i = 1; i < s.length(); ++i) res[i] = res[i-1]*t, t*=m; return res;}
ull subHash(vecull &v, ull l, ull r) {return (v[r] - v[l - 1]);}

#ifdef _MSC_VER
#define typeof(a) decltype(a)
#endif
#define sqr(x) ((x)*(x))
#define fru(i, n) for (int (i) = 0; i < (n); ++i)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define frd(i, n) for (int (i) = n - 1; i >= 0; --i)
#define rdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define rd(name, type) type name; cin >> name;
#define rds(name) string name; getline(cin, name);
#define rd2(name1, type1, name2, type2) rd(name1, type1); rd(name2, type2);
#define rd3(n1, t1, n2, t2, n3, t3) rd2(n1, t1, n2, t2); rd(n3, t3);
#define rd4(n1, t1, n2, t2, n3, t3, n4, t4) rd2(n1, t1, n2, t2); rd2(n3, t3, n4, t4);
#define each(e,s) for(typeof((s).begin()) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define summ(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec, restype) restype name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define F first
#define S second
#define pb push_back

vecll a(500005);
vecll f(500005), b(500005);
int n;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin >> n;
    cin >> a[0];
    f[0] = a[0];
    for (int i = 1; i < n; i++)
    {
        cin >> a[i];
        f[i] = f[i-1] + a[i];
    }
    b[n-1] = a[n-1];
    for (int i = 1; i < n; i++)
    {
        b[n-1-i] = b[n-i] + a[n-i-1];
    }
    int ans = 0, s2;
    fru(i, n - 2)
    {
        for (int j = i+1; j < n - 1; ++j)
        {
            s2 = f[n-1] - f[i] - b[j+1];
            ans += (f[i] == s2 && s2 == b[j+1]);
        }
    }
    cout << ans;
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdarg.h>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<ll> vecll;
typedef vector<db> vecd;
typedef vector<vector<int> > veci2;
typedef vector<pair<int, bool> > vecif;
typedef vector<bool> vecb;
typedef vector<string> vecstr;
typedef vector<pair<int, int> > vecprii;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef map<char,bool> mapcb;
typedef map<char,int> mapci;
typedef map<int, bool> mapib;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
template<class T, class T2> bool cmpfirst(const pair<T, T2> &a, const pair<T, T2> &b) { return a.first > b.first; }
template<class T, class T2> bool cmpsecond(const pair<T, T2> &a, const pair<T, T2> &b) { return a.second > b.second; }
template<class T> inline T abs(T t) {return ((t<0)?(-t):t);}
template<class T> inline T maxv(vector<T> v) { return *(max_element(v.begin(), v.end())); };
template<class T> inline T minv(vector<T> v) { return *(min_element(v.begin(), v.end())); };
template<class T> T gcd(T a, T b) {return ((a == 0)?b:(b == 0)?a:((a > b)?gcd(a - b, b):(a < b)?gcd(a, b - a):a));}
template<class T> T gcd(T a, T b, T c) {return gcd(a, gcd(b, c));}
template<class T> T gcd(T a, T b, T c, T d) {return gcd(gcd(a, d), gcd(b, c));}
template<class T> T lcm(T a, T b) {return (a * b)/gcd(a, b);}
template<class T1, class T2> pair<T1, T2> mp(T1 t1, T2 t2) {return make_pair(t1, t2);}
template<class T> bool in(T a, T min, T max) {return ((a>min && a<max));}
template<class T> bool out(T a, T min, T max) {return ((a<min || a>max));}
template<class T> T sqr(T &a) {return a*a;}
inline bool isUpperCase(char c) {return (c >= 'A' && c <= 'Z');}
inline bool isLowerCase(char c) {return ( c>= 'a' && c <= 'z');}
inline bool isDigit(char c) {return (c >= '0' && c <= '9');}
inline bool isLetter(char c) {return (isUpperCase(c) || isLowerCase(c));}
inline char toLowerCase(char c) {return ((isUpperCase(c))?c+32:c);}
inline char toUpperCase(char c) {return ((isLowerCase(c))?c-32:c);}
vecull hashString(string s) {vecull res(s.length()); ull m = 17431, t = m; res[0] = s[0]; for (int i = 1; i < s.length(); ++i) res[i] = res[i-1]*t, t*=m; return res;}
ull subHash(vecull &v, ull l, ull r) {return (v[r] - v[l - 1]);}

#ifdef _MSC_VER
#define typeof(a) decltype(a)
#endif
#define sqr(x) ((x)*(x))
#define fru(i, n) for (int (i) = 0; i < (n); ++i)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define frd(i, n) for (int (i) = n - 1; i >= 0; i--)
#define rdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define rd(name, type) type name; cin >> name;
#define rds(name) string name; getline(cin, name);
#define rd2(name1, type1, name2, type2) rd(name1, type1); rd(name2, type2);
#define rd3(n1, t1, n2, t2, n3, t3) rd2(n1, t1, n2, t2); rd(n3, t3);
#define rd4(n1, t1, n2, t2, n3, t3, n4, t4) rd2(n1, t1, n2, t2); rd2(n3, t3, n4, t4);
#define each(e,s) for(typeof((s).begin()) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define summ(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec, restype) restype name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define F first
#define S second
#define pb push_back

veci a(100005);
veci t(200005);
int n;

int sum (int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    return sum (v*2, tl, tm, l, min(r,tm))
        + sum (v*2+1, tm+1, tr, max(l,tm+1), r);
}
int sum(int l, int r)
{
    return sum(1, 0, n-1, l, r);
}

void build (veci a, int v, int tl, int tr) {
    if (tl == tr)
        t[v] = a[tl];
    else {
        int tm = (tl + tr) / 2;
        build (a, v*2, tl, tm);
        build (a, v*2+1, tm+1, tr);
        t[v] = t[v*2] + t[v*2+1];
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin >> n;
    fru(i, n)
    {
        cin >> a[i];
    }
    build(a, 1, 0, n-1);
    int ans = 0, s1, s2, s3;
    fru(i, n)
    {
        for (int j = i+1; j < n; j++)
        {
            s1 = sum(0, i);
            s2 = sum(i+1, j);
            s3 = sum(j+1, n-1);
            ans += (s1 == s2 && s2 == s3);
        }
    }
    cout << ans;
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdarg.h>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<ll> vecll;
typedef vector<db> vecd;
typedef vector<vector<int> > veci2;
typedef vector<pair<int, bool> > vecif;
typedef vector<bool> vecb;
typedef vector<string> vecstr;
typedef vector<pair<int, int> > vecprii;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef map<char,bool> mapcb;
typedef map<char,int> mapci;
typedef map<int, bool> mapib;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
template<class T, class T2> bool cmpfirst(const pair<T, T2> &a, const pair<T, T2> &b) { return a.first > b.first; }
template<class T, class T2> bool cmpsecond(const pair<T, T2> &a, const pair<T, T2> &b) { return a.second > b.second; }
template<class T> inline T abs(T t) {return ((t<0)?(-t):t);}
template<class T> inline T maxv(vector<T> v) { return *(max_element(v.begin(), v.end())); };
template<class T> inline T minv(vector<T> v) { return *(min_element(v.begin(), v.end())); };
template<class T> T gcd(T a, T b) {return ((a == 0)?b:(b == 0)?a:((a > b)?gcd(a - b, b):(a < b)?gcd(a, b - a):a));}
template<class T> T gcd(T a, T b, T c) {return gcd(a, gcd(b, c));}
template<class T> T gcd(T a, T b, T c, T d) {return gcd(gcd(a, d), gcd(b, c));}
template<class T> T lcm(T a, T b) {return (a * b)/gcd(a, b);}
template<class T1, class T2> pair<T1, T2> mp(T1 t1, T2 t2) {return make_pair(t1, t2);}
template<class T> bool in(T a, T min, T max) {return ((a>min && a<max));}
template<class T> bool out(T a, T min, T max) {return ((a<min || a>max));}
template<class T> T sqr(T &a) {return a*a;}
inline bool isUpperCase(char c) {return (c >= 'A' && c <= 'Z');}
inline bool isLowerCase(char c) {return ( c>= 'a' && c <= 'z');}
inline bool isDigit(char c) {return (c >= '0' && c <= '9');}
inline bool isLetter(char c) {return (isUpperCase(c) || isLowerCase(c));}
inline char toLowerCase(char c) {return ((isUpperCase(c))?c+32:c);}
inline char toUpperCase(char c) {return ((isLowerCase(c))?c-32:c);}
vecull hashString(string s) {vecull res(s.length()); ull m = 17431, t = m; res[0] = s[0]; for (int i = 1; i < s.length(); ++i) res[i] = res[i-1]*t, t*=m; return res;}
ull subHash(vecull &v, ull l, ull r) {return (v[r] - v[l - 1]);}

#ifdef _MSC_VER
#define typeof(a) decltype(a)
#endif
#define sqr(x) ((x)*(x))
#define fru(i, n) for (int (i) = 0; i < (n); ++i)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define frd(i, n) for (int (i) = n - 1; i >= 0; i--)
#define rdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define rd(name, type) type name; cin >> name;
#define rds(name) string name; getline(cin, name);
#define rd2(name1, type1, name2, type2) rd(name1, type1); rd(name2, type2);
#define rd3(n1, t1, n2, t2, n3, t3) rd2(n1, t1, n2, t2); rd(n3, t3);
#define rd4(n1, t1, n2, t2, n3, t3, n4, t4) rd2(n1, t1, n2, t2); rd2(n3, t3, n4, t4);
#define each(e,s) for(typeof((s).begin()) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define summ(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec, restype) restype name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define F first
#define S second
#define pb push_back

veci a(200005);
int n;

int sum (int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr)
        return a[v];
    int tm = (tl + tr) / 2;
    return sum (v*2, tl, tm, l, min(r,tm))
        + sum (v*2+1, tm+1, tr, max(l,tm+1), r);
}
int sum(int l, int r)
{
    return sum(1, 0, n-1, l, r);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin >> n;
    fru(i, n)
    {
        cin >> a[n+i];
    }
    frd(i, n)
    {
        a[i] = a[i*2]+a[i*2+1];
    }
    int ans = 0, s1, s2, s3;
    fru(i, n)
    {
        for (int j = i+1; j < n; j++)
        {
            for (int k = j+1; k < n; k++)
            {
                s1 = sum(i, j-1);
                s2 = sum(j, k-1);
                s3 = sum(k, n-1);
                ans += (s1 == s2 && s2 == s3);
            }
        }
    }
    cout << ans;
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <math.h>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdarg.h>
#include <conio.h>
#include <streambuf>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef vector<int> veci;
typedef vector<ull> vecull;
typedef vector<ll> vecll;
typedef vector<db> vecd;
typedef vector<vector<int> > veci2;
typedef vector<pair<int, bool> > vecif;
typedef vector<bool> vecb;
typedef vector<string> vecstr;
typedef vector<pair<int, int> > vecprii;
typedef pair<int,int> prii;
typedef map<int,int> mapii;
typedef map<char,bool> mapcb;
typedef map<char,int> mapci;
typedef map<int, bool> mapib;
typedef set<int> seti;
typedef multiset<int> mseti;

const int mod = 1e9 + 7;
template<class T, class T2> bool cmpfirst(const pair<T, T2> &a, const pair<T, T2> &b) { return a.first > b.first; }
template<class T, class T2> bool cmpsecond(const pair<T, T2> &a, const pair<T, T2> &b) { return a.second > b.second; }
template<class T> inline T abs(T t) {return ((t<0)?(-t):t);}
template<class T> inline T maxv(vector<T> v) { return *(max_element(v.begin(), v.end())); };
template<class T> inline T minv(vector<T> v) { return *(min_element(v.begin(), v.end())); };
template<class T> T gcd(T a, T b) {return ((a == 0)?b:(b == 0)?a:((a > b)?gcd(a - b, b):(a < b)?gcd(a, b - a):a));}
template<class T> T gcd(T a, T b, T c) {return gcd(a, gcd(b, c));}
template<class T> T gcd(T a, T b, T c, T d) {return gcd(gcd(a, d), gcd(b, c));}
template<class T> T lcm(T a, T b) {return (a * b)/gcd(a, b);}
template<class T1, class T2> pair<T1, T2> mp(T1 t1, T2 t2) {return make_pair(t1, t2);}
template<class T> bool in(T a, T min, T max) {return ((a>min && a<max));}
template<class T> bool out(T a, T min, T max) {return ((a<min || a>max));}
template<class T> T sqr(T &a) {return a*a;}
inline bool isUpperCase(char c) {return (c >= 'A' && c <= 'Z');}
inline bool isLowerCase(char c) {return ( c>= 'a' && c <= 'z');}
inline bool isDigit(char c) {return (c >= '0' && c <= '9');}
inline bool isLetter(char c) {return (isUpperCase(c) || isLowerCase(c));}
inline char toLowerCase(char c) {return ((isUpperCase(c))?c+32:c);}
inline char toUpperCase(char c) {return ((isLowerCase(c))?c-32:c);}
//vecull hashString(string s) {vecull res(s.length()); ull m = 17431, t = m; res[0] = s[0]; for (int i = 1; i < s.length(); ++i) res[i] = res[i-1]*t, t*=m; return res;}
//ull subHash(vecull &v, ull l, ull r) {return (v[r] - v[l - 1]);}

#ifdef _MSC_VER
#define typeof(a) decltype(a)
#endif
#define sqr(x) ((x)*(x))
#define fru(i, n) for (int (i) = 0; i < (n); ++i)
#define frus(i, st, n, inc) for (int (i) = (st); (i) < (n); (i)+=(inc))
#define frd(i, n) for (int (i) = n - 1; i >= 0; --i)
#define rdvec(name, type, size) vector<type> name(size); fru(zzzz, size) {cin >> name[zzzz];}
#define rd(name, type) type name; cin >> name;
#define rds(name) string name; getline(cin, name);
#define rd2(name1, type1, name2, type2) rd(name1, type1); rd(name2, type2);
#define rd3(n1, t1, n2, t2, n3, t3) rd2(n1, t1, n2, t2); rd(n3, t3);
#define rd4(n1, t1, n2, t2, n3, t3, n4, t4) rd2(n1, t1, n2, t2); rd2(n3, t3, n4, t4);
#define each(e,s) for(typeof((s).begin()) e=(s).begin(); e!=(s).end(); ++e)
#define toeach(s,add) { each(e, s) (*e)+=add; }
#define summ(s, n) { s+=(n); if (s > mod) s-=mod; }
#define oiarr(a, n) { fru(zzzz, n) cout << a[zzzz] << " "; cout << endl; }
#define iiarr(a, n) { fru(zzzz, n) cin >> a[zzzz]; }
#define srtd(a) { sort((a).begin(), (a).end(), std::greater<typeof((a).front())>()); }
#define srtu(a) { sort((a).begin(), (a).end(), std::less<typeof((a).front())>()); }
#define summvec(name, vec, restype) restype name = 0; fru(zzzz, vec.size()) name += vec[zzzz];
#define F first
#define S second
#define pb push_back

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	rd(n, int);
	vecll a(n);
	vecll f(n);
	cin >> a[0];
	f[0] = a[0];
	for (int i = 1; i < n; ++i)
	{
		cin >> a[i];
		f[i] = f[i-1] + a[i];
	}
	ll ans = 0, k = 0;
	for (int i = 0; i < n-1; i++)
	{
		if (f[i]*3 == f[n-1]*2) ans+=k;
		if (f[i]*3 == f[n-1]) k++;
	}
	cout << ans;
	return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <deque>
#include <queue>
#include <iomanip>
#include <limits>

using namespace std;

#define sz(v) v.size()
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll 	long long
#define scni(x)	scanf("%d",&x)
#define scnll(x)	scanf("%lld",&x)
#define scnd(x)	scanf("%lf",&x)
#define scnc(x)	scanf(" %c",&x)
#define prnti(x)	printf("%d\n",x)
#define prntll(x)	printf("%lld",ll(x))
#define prntd(x)	printf("%.9f",x)
#define lop(i , n) for(int i = 0 ; i < n ; i++)
#define P pair<int ,int>
#define pp push_back
#define mp make_pair
#define VI	vector<int>
#define VC	vector<char>
#define VD	vector<double>
#define VB	vector<bool>
#define SPC << " "
#define INF 1e9

ll GCD(ll a ,ll b){return (b==0)? a : GCD(b , a%b );}

bool isPrime(int x){

	for(int i = 2 ; i <= sqrt(x);i++ )if(x % i == 0)return 0;
	return 1;
}

#define MOD ll(1e9 + 7)

//  1 .. 2 .. 3 ..

int C[26 * 3];
bool have[27];
double dist(int a ,int b ,int i ,int y){
	return sqrt(pow(a - i,2) + pow(b - y,2)) ;
}

int main (){

	//freopen("input.txt",  "rt", stdin);
	//freopen("output.txt", "wt", stdout);
	int n , m , x , t;
	cin >> n >> m >> x;

	vector<string> v(n);
	vector<P> S;
	lop(i , n){
		string s;
		cin >> s;
		lop(k , m){ if(s[k] == 'S'){have[26]=1;S.pp(mp(i , k));} else have[s[k]-'a']=1; }
		v[i]=s;
	}

	string s;
	cin >>t >> s;
	bool bad=0;
	lop(i , t) {
		char c = s[i];
		C[c-'A']++;
		if(have[tolower(c)-'A']==0){bad=1; break;}
		if(c < 'a'){
			if(have[26]==0){bad=1;break;}
		}
	}
	if(bad){cout << -1 ; return 0;}

	int ans =0 ;
	lop(i , n){
		lop(j , m){
			if(C[toupper(v[i][j])-'A']==0 )continue;
			else {
				double d=99999999;
				lop(k , sz(S)){
					d = min(dist( i , j , S[k].first , S[k].second ) , d);
				}
				if(d > x)ans+=C[toupper(v[i][j])-'A'];
			}
			C[v[i][j]-'A']=0;
			C[toupper(v[i][j])-'A']=0;
		}
	}
	cout << ans;
return 0 ;}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <deque>
#include <queue>
#include <iomanip>
#include <limits>

using namespace std;

#define sz(v) v.size()
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll 	long long
#define scni(x)	scanf("%d",&x)
#define scnll(x)	scanf("%lld",&x)
#define scnd(x)	scanf("%lf",&x)
#define scnc(x)	scanf(" %c",&x)
#define prnti(x)	printf("%d\n",x)
#define prntll(x)	printf("%lld",ll(x))
#define prntd(x)	printf("%.9f",x)
#define lop(i , n) for(int i = 0 ; i < n ; i++)
#define P pair<int ,int>
#define pp push_back
#define mp make_pair
#define VI	vector<int>
#define VC	vector<char>
#define VD	vector<double>
#define VB	vector<bool>
#define SPC << " "
#define INF 1e9

ll GCD(ll a ,ll b){return (b==0)? a : GCD(b , a%b );}

bool isPrime(int x){

	for(int i = 2 ; i <= sqrt(x);i++ )if(x % i == 0)return 0;
	return 1;
}

#define MOD ll(1e9 + 7)

//  1 .. 2 .. 3 ..

int C[26 * 3];
bool have[27];
double dist(int a ,int b ,int i ,int y){
	return sqrt(pow(a - i,2) + pow(b - y,2)) ;
}

int main (){

	//freopen("input.txt",  "rt", stdin);
	//freopen("output.txt", "wt", stdout);
	int n , m , x , t;
	cin >> n >> m >> x;

	vector<string> v(n);
	vector<P> S;
	lop(i , n){
		string s;
		cin >> s;
		lop(k , m){ if(s[k] == 'S'){have[26]=1;S.pp(mp(i , k));} else have[s[k]-'a']=1; }
		v[i]=s;
	}

	string s;
	cin >>t >> s;
	bool bad=0;
	lop(i , t) {
		char c = s[i];
		C[c-'A']++;
		if(have[tolower(c)-'a']==0){bad=1; break;}
		if(c < 'a'){
			if(have[26]==0){bad=1;break;}
		}
	}
	if(bad){cout << -1 ; return 0;}

	int ans =0 ;
	lop(i , n){
		lop(j , m){
			if(C[toupper(v[i][j])-'A']==0 )continue;
			else {
				double d=99999999;
				lop(k , sz(S)){
					d = min(dist( i , j , S[k].first , S[k].second ) , d);
				}
				if(d > x)ans+=C[toupper(v[i][j])-'A'];
			}
			C[v[i][j]-'A']=0;
			C[toupper(v[i][j])-'A']=0;
		}
	}
	cout << ans;
return 0 ;}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <deque>
#include <queue>
#include <iomanip>
#include <limits>

using namespace std;

#define sz(v) v.size()
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll 	long long
#define scni(x)	scanf("%d",&x)
#define scnll(x)	scanf("%lld",&x)
#define scnd(x)	scanf("%lf",&x)
#define scnc(x)	scanf(" %c",&x)
#define prnti(x)	printf("%d\n",x)
#define prntll(x)	printf("%lld",ll(x))
#define prntd(x)	printf("%.9f",x)
#define lop(i , n) for(int i = 0 ; i < n ; i++)
#define P pair<int ,int>
#define pp push_back
#define mp make_pair
#define VI	vector<int>
#define VC	vector<char>
#define VD	vector<double>
#define VB	vector<bool>
#define SPC << " "
#define INF 1e9

ll GCD(ll a ,ll b){return (b==0)? a : GCD(b , a%b );}

bool isPrime(int x){

	for(int i = 2 ; i <= sqrt(x);i++ )if(x % i == 0)return 0;
	return 1;
}

#define MOD ll(1e9 + 7)

//  1 .. 2 .. 3 ..

int C[26 * 3];
bool have[27];
double dist(int a ,int b ,int i ,int y){
	return sqrt(pow(a - i,2) + pow(b - y,2)) ;
}

int main (){

	//freopen("input.txt",  "rt", stdin);
	//freopen("output.txt", "wt", stdout);
	int n , m , x , t;
	cin >> n >> m >> x;

	vector<string> v(n);
	vector<P> S;
	lop(i , n){
		string s;
		cin >> s;
		lop(k , m){ if(s[k] == 'S'){have[26]=1;S.pp(mp(i , k));} else have[s[k]-'a']=1; }
		v[i]=s;
	}

	string s;
	cin >>t >> s;
	bool bad=0;
	lop(i , t) {if(s[i]<'a'){if(have[26]==0)bad=1;}
		else if(have[s[i]-'a']==0)bad=1;
		C[s[i] - 'A']++;}
	if(bad){cout << -1 ; return 0;}

	int ans =0 ;
	lop(i , n){
		lop(j , m){
			if(C[toupper(v[i][j])-'A']==0 )continue;
			else {
				double d=99999999;
				lop(k , sz(S)){
					d = min(dist( i , j , S[k].first , S[k].second ) , d);
				}
				if(d > x)ans+=C[toupper(v[i][j])-'A'];
			}
		}
	}
	cout << ans;
return 0 ;}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <deque>
#include <queue>
#include <iomanip>
#include <limits>

using namespace std;

#define sz(v) v.size()
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll 	long long
#define scni(x)	scanf("%d",&x)
#define scnll(x)	scanf("%lld",&x)
#define scnd(x)	scanf("%lf",&x)
#define scnc(x)	scanf(" %c",&x)
#define prnti(x)	printf("%d\n",x)
#define prntll(x)	printf("%lld",ll(x))
#define prntd(x)	printf("%.9f",x)
#define lop(i , n) for(int i = 0 ; i < n ; i++)
#define P pair<int ,int>
#define pp push_back
#define mp make_pair
#define VI	vector<int>
#define VC	vector<char>
#define VD	vector<double>
#define VB	vector<bool>
#define SPC << " "
#define INF 1e9

ll GCD(ll a ,ll b){return (b==0)? a : GCD(b , a%b );}

bool isPrime(int x){

	for(int i = 2 ; i <= sqrt(x);i++ )if(x % i == 0)return 0;
	return 1;
}

#define MOD ll(1e9 + 7)

//  1 .. 2 .. 3 ..

int C[26 * 3];
bool have[27];
double dist(int a ,int b ,int i ,int y){
	return sqrt(pow(a - i,2) + pow(b - y,2)) ;
}

int main (){

	//freopen("input.txt",  "rt", stdin);
	//freopen("output.txt", "wt", stdout);
	int n , m , x , t;
	cin >> n >> m >> x;

	vector<string> v(n);
	vector<P> S;
	lop(i , n){
		string s;
		cin >> s;
		lop(k , m){ if(s[k] == 'S'){have[26]=1;S.pp(mp(i , k));} else have[s[k]-'a']=1; }
		v[i]=s;
	}

	string s;
	cin >>t >> s;
	bool bad=0;
	lop(i , t) {
		char c = s[i];
		C[c-'A']++;
		if(have[tolower(c)-'a']==0){bad=1; break;}
		if(c < 'a'){
			if(have[26]==0){bad=1;break;}
		}
	}
	if(bad){cout << -1 ; return 0;}
	vector<VI>anss(26);
	int ans =0 ;
	lop(i , n){
		lop(j , m){
			if(C[toupper(v[i][j])-'A']==0 || v[i][j]=='S' )continue;
			else {
				double d=99999999;
				lop(k , sz(S)){
					d = min(dist( i , j , S[k].first , S[k].second ) , d);
				}
					if(d > x)ans+=C[toupper(v[i][j])-'A'];
					anss[v[i][j]-'a'].pp(ans);
					ans=0;

			}
		}
	}
	ans=0;
	lop(i , 26){
		SORT(anss[i]);
		if(sz(anss[i]))ans+=anss[i][0];
	}

	cout << ans;
return 0 ;}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(ll i=a; i<b; i++)
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
int distance(pair<int,int>i, pair<int,int>j){
    return ceil(sqrt((i.first-j.first)*(i.first-j.first)+ (i.second-j.second)*(i.second-j.second)));
}

int main(){
    ios;
    ll t=1;  while(t--){
        int n,m,x; cin>>n>>m>>x;
        map<char,vector<pair<int,int>>>mp;
        map<char,int>sdis;
        f(i,0,n)f(j,0,m){ char c; cin>>c; mp[c].pb(mk(i,j));}
        for(auto u:mp){
            int dis=10000;
            for(auto i:mp['S']){
                for(auto j:mp[u.first]){
                    dis=min(dis,distance(i,j));
                }
            }
            sdis[toupper(u.first)]=dis;
        }
        int q,ans=0;
        while(q--){
            char c; cin>>c;
            if(c<'a'&&mp['S'].empty() || mp.find(tolower(c))==mp.end())
                {cout<<-1<<endl; return 0;}
            if(c<'a'&&sdis[c]>x)ans++;

        }
        cout<<ans;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(ll i=a; i<b; i++)
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
vector<pair<int,int>>shift;
int n,m,x;

float distance(int a, int b){
    float dis=10000;
    for(auto u:shift){
        float temp=sqrt((a-u.first)*(a-u.first) + (b-u.second)*(b-u.second));
        dis=min(temp,dis);
    }
    return dis;

}

int main(){
    ios;
    ll t=1;  while(t--){
    cin>>n>>m>>x;
    set<char>check;
    char arr[n][m];
    f(i,0,n)f(j,0,m)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')shift.pb(mk(i,j));
            check.insert(arr[i][j]);
        }
    int q; string s; cin>>q>>s;
    int ans;
    for(auto u:s){
        if((check.find(tolower(u))==check.end())||(isupper(u)&& shift.size()==0))
            {ans=-1; break;}

        if(isupper(u)){
            int a,b;
            f(i,0,n)f(j,0,m)if(arr[i][j]==tolower(u)) {a=i; b=j;}
            float dis=distance(a,b);
            if(dis>x)ans++;
        }

    }
    cout<<ans;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(ll i=a; i<b; i++)
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
vector<pair<int,int>>shift;
int n,m,x;

float distance(int a, int b){
    float dis=10000;
    for(auto u:shift){
        float temp=sqrt((a-u.first)*(a-u.first) + (b-u.second)*(b-u.second));
        dis=min(temp,dis);
    }
    return dis;

}

int main(){
    ios;
    ll t=1;  while(t--){
    cin>>n>>m>>x;
    set<char>check;
    char arr[n][m];
    f(i,0,n)f(j,0,m)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')shift.pb(mk(i,j));
            check.insert(arr[i][j]);
        }
    int q; string s; cin>>q>>s;
    int ans=0;
    for(auto u:s){
        if((check.find(tolower(u))==check.end())||(isupper(u)&& shift.size()==0))
            {ans=-1; break;}

        if(isupper(u)){
           float dis=10000;
            f(i,0,n)f(j,0,m)
                if(arr[i][j]==tolower(u)){
                    int a,b; a=i; b=j;
                    dis=min(dis,distance(a,b));
                }
            if(dis>x)ans++;
        }

    }
    cout<<ans;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(ll i=a; i<b; i++)
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
int distance(pair<int,int>i, pair<int,int>j){
    return ceil(sqrt((i.first-j.first)*(i.first-j.first)+ (i.second-j.second)*(i.second-j.second)));
}

int main(){
    ios;
    ll t=1;  while(t--){
        int n,m,x; cin>>n>>m>>x;
        map<char,vector<pair<int,int>>>mp;
        map<char,int>sdis;
        f(i,0,n)f(j,0,m){ char c; cin>>c; mp[c].pb(mk(i,j));}
        for(auto u:mp){
            int dis=10000;
            for(auto i:mp['S']){
                for(auto j:mp[u.first]){
                    dis=min(dis,distance(i,j));
                }
            }
            sdis[toupper(u.first)]=dis;
        }
        int q,ans=0; cin>>q;
        while(q--){
            char c; cin>>c;
            if(c<'a'&&mp['S'].empty() || mp.find(tolower(c))==mp.end())
                {cout<<-1<<endl; return 0;}
            if(c<'a'&&sdis[c]>x)ans++;

        }
        cout<<ans;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(ll i=a; i<b; i++)
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define pb push_back
#define mk make_pair
vector<pair<int,int>>shift;
int n,m,x;

float distance(int a, int b){
    float dis=10000;
    for(auto u:shift){
        float temp=sqrt((a-u.first)*(a-u.first) + (b-u.second)*(b-u.second));
        dis=min(temp,dis);
    }
    return dis;

}

int main(){
    ios;
    ll t=1;  while(t--){
    cin>>n>>m>>x;
    set<char>check;
    char arr[n][m];
    f(i,0,n)f(j,0,m)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='S')shift.pb(mk(i,j));
            check.insert(arr[i][j]);
        }
    int q; string s; cin>>q>>s;
    int ans=0;
    for(auto u:s){
        if((check.find(tolower(u))==check.end())||(isupper(u)&& shift.size()==0))
            {ans=-1; break;}

        if(isupper(u)){
            int a,b;
            f(i,0,n)f(j,0,m)if(arr[i][j]==tolower(u)) {a=i; b=j;}
            float dis=distance(a,b);
            if(dis>x)ans++;
        }

    }
    cout<<ans;

    }
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <ctype.h>
#include <cmath>

using namespace std;

int main() {
	int n, m, x;
	cin >> n >> m >> x;
	char a[30][30];
	bool key[26];
	vector< pair<int,int> > s;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] != 'S') key[a[i][j] - 'a'] = true;
			else s.push_back(make_pair(i, j));
		}
	}
	int q;
	cin >> q;
	string text;
	cin >> text;
	int hands = 0;
	for (int i = 0; i < q; i++) {
		char c =  text[i];
		if (islower(c)) {
			if(!key[c-'a']) {
				cout << -1;
				return 0;
			}
		} else {
			c = tolower(c);
			bool type = false;
			for (int j = 0; j < n; j++) {
				for (int k = 0; k < m; k++) {
					if (a[j][k] == c) {
						for (int l = 0; l < s.size(); l++) {
							double dis = sqrt(pow(j - s[l].first, 2) + pow(k - s[l].second,2));
							if (dis <= x) type = true;
						}
					}
				}
			}
			if (s.empty() || !key[c-'a']) {
				cout << -1;
				return 0;
			}
			else {
				if (!type) hands++;
			}
		}
	}
	cout << hands;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <ctype.h>
#include <cmath>

using namespace std;

int main() {
	int n, m, x;
	cin >> n >> m >> x;
	x *= x;
	char a[30][30];
	bool key[26] = {false};
	bool used[26] = {false};

	vector< pair<int, int> > s;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] != 'S') key[a[i][j] - 'a'] = true;
			else s.push_back(make_pair(i, j));
		}
	}
	bool shift = !s.empty();

	for (int i = 0; i < s.size(); i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < m; k++) {
				if (a[j][k] != 'S') {
					double dis = pow(s[i].first - j, 2) + pow(s[i].second - k, 2);
					if (dis < x) used[a[j][k] - 'a'] = true;
				}
			}
		}
	}

	int q;
	cin >> q;
	string text;
	cin >> text;
	int hands = 0;
	for (int i =0; i < q; i++) {
		char c = text[i];
		if(islower(c)) {
			if (!key[c - 'a']) {
				cout << -1;
				return 0;
			}
		} else {
			if (!key[c - 'A'] || !shift) {
				cout << -1;
				return 0;
			} else if (!used[c - 'A']) hands++;
		}
	}
	cout << hands;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>
#include <ctype.h>
#include <cmath>

using namespace std;

int main() {
	int n, m, x;
	cin >> n >> m >> x;
	x *= x;
	char a[30][30];
	bool key[26] = {false};
	bool used[26] = {false};

	vector< pair<int, int> > s;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] != 'S') key[a[i][j] - 'a'] = true;
			else s.push_back(make_pair(i, j));
		}
	}
	bool shift = !s.empty();

	for (int i = 0; i < s.size(); i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < m; k++) {
				if (a[j][k] != 'S') {
					double dis = pow(s[i].first - j, 2) + pow(s[i].second - k, 2);
					if (dis <= x) used[a[j][k] - 'a'] = true;
				}
			}
		}
	}

	int q;
	cin >> q;
	string text;
	cin >> text;
	int hands = 0;
	for (int i =0; i < q; i++) {
		char c = text[i];
		if(islower(c)) {
			if (!key[c - 'a']) {
				cout << -1;
				return 0;
			}
		} else {
			if (!key[c - 'A'] || !shift) {
				cout << -1;
				return 0;
			} else if (!used[c - 'A']) hands++;
		}
	}
	cout << hands;
	return 0;
}