#include <iostream>
#include <stdio.h>
#include <string>
#include <list>
#include <vector>
#include <unordered_map>
#include <cmath>

using namespace std;

int i1;
int fsqrt(int pos, string rad1, string rad2);
int fract(string rad1, string rad2,string rad3, int pos);
	string rad[3];
int term(string vyr, int pos, int ab)
{
	// ab * (cd)
	
	i1 = pos+2;
	
	int mez = 0;
	
	while(vyr[i1] <= '9' && vyr[i1] >= '0')
	{
		mez *= 10;
		mez += vyr[i1] - '0';
		i1++;
	} 
	
	return mez*ab;
}

int simple(string vyr, int pos, char pred, int fin)
{
	int pamet = 0;
	int mez = 0;
	i1 = pos;
	while( i1 <= fin)
	{
		if(vyr[i1] != ' ' && i1 <= vyr.length())
		{			
			if(vyr[i1] <= '9' && vyr[i1] >= '0')
			{
				mez *= 10;
				mez += vyr[i1] - '0';
			}
			if(vyr[i1] == '+' || vyr[i1] == '-')
			{
				if(pred == '+') 
				{
					pamet += mez;		
				}
				else
				{
					pamet -= mez;
				}
				pred = vyr[i1];
				mez = 0;
			}
			if(vyr[i1] == '*')
			{
				mez = term(vyr,i1,mez);
			}
			if(vyr[i1] == '\\')
			{
				mez = fsqrt(i1,rad[0],rad[1]);
			}
			if(vyr[i1] == '=')
			{
				mez = fract(rad[0],rad[1],rad[2],i1);
			}
		}
		i1++;
	}
	if(pred == '+')
	{
	pamet += mez;	pred = '+';			
	}
	else
	{
	pamet -= mez; pred = '-';
	}
	
	return pamet;
}

int fract(string rad1, string rad2,string rad3, int pos)
{
	int i = pos;	
	while(rad2[i] == '=')
	{
		i++;
	}
	i--; // posledni rovnase
	
	int c = simple(rad1, pos, '+',i);
	int j = simple(rad3, pos, '+',i);
	return c/j;
}

int fsqrt(int pos, string rad1, string rad2)
{
	int i = pos+2;	
	while(rad1[i] == '_')
	{
		i++;
	}
	i--; // posledni rovnase

	int res = simple(rad2,pos+2,'+',i);
	
	return sqrt(res);
}

int formule(int n, string rad[3])
{
	int res = 0;
	
	if(n == 1)
	{
		res = simple(rad[0], 0, '+',rad[0].length());
	}
	if(n > 1)
	{
		res = simple(rad[1], 0, '+',rad[1].length());
	}
	return res;
}

int main()
{
	int N,M;
	cin >> N >> M;
	string nic;
	while(N > 0)
	{
		getline(cin, nic);
		//cout << "a" << endl;
		getline(cin,rad[0]);	
		//cout << rad[0] << endl;
		
		if(N >= 2) { getline(cin,rad[1]); }
		if(N >= 3) { getline(cin,rad[2]); }				
		cout << formule(N,rad) << endl;		
		i1 = 0;		
		cin >> N >> M;
	}	
	
	// nothing follows
	/*
	string m = "    6 * 5";
	string n = "1 - =====";
	string g = "      2  ";
	*/
//	cout << simple(n,0,'+',n.length());
	//cout << fract(m,n,g,0,m.length());
	/*
	string ac = "  _________";
	string ab = "\\/5 * 6 + 6";
	cout << sqrt(0,ac,ab);
*/	
/*
	rad[0] = "    22     __";
	rad[1] = "3 - == - \\/16";
	rad[2] = "    11       ";
	cout <<  formule(3,rad);
	*/
	return 0;
}