#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reg(i,a,b) for(int i=(a);i<=(b);i++)
#define irep(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define ireg(i,a,b) for(int i=(b);i>=(a);i--)
typedef long long int lli;
typedef pair<int,int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX


int w,h;
char s[25][100]; // half close

void err(const char* ers){
	printf("onerr %s\n",ers);
}

const int MOD = 2011;

int modpow(int p,int q){
	if(q==0)return 1;
	int res = modpow(p,q/2);
	res = (res*res)%MOD;
	if(q%2==1)res = (res*p)%MOD;
	return res;
}

int modinv(int p){
	return modpow(p,MOD-2);
}

/*
?????¶?????¨???
????????¨????????????????????????????????¨????????§?????????????????????

?????????????????????...fraction ??¨ expr
*/

/*
??????????
*/
int expr(int& t,int& b,int& l,int& r);
int term(int& t,int& b,int& l,int& r);
int factor(int& t,int& b,int& l,int& r);
int powexpr(int& t,int& b,int& l,int& r);
int primary(int& t,int& b,int& l,int& r);
int fraction(int t,int b,int l,int r);

//#define pospr(is) printf("%10s .. t..%2d b..%2d l..%2d r..%2d\n",is,t,b,l,r)
#define pospr(is) ;

int expr(int& t,int& b,int& l,int& r){
	pospr("expr");
	int res = term(t,b,l,r);
	
	for(;;){
		if(r<l+3)return res; // if l.r
		
		bool vd = true;
		int ind = 0;
		reg(y,t,b){
			if(s[y][l]!='.' || s[y][l+2]!='.'){
				vd = false;
			}
			if(s[y][l+1]!='.'){
				if(ind!=0)vd = false;
				else if(s[y][l+1]=='+')ind = 1;
				else if(s[y][l+1]=='-')ind = 2;
				else vd = false;
			}
		}
		if(ind == 0 || !vd)return res%MOD;
		
		l+=3;
		int nr = term(t,b,l,r);
		
		if(ind==1)res=(res+nr)%MOD;
		else res=(res-nr+MOD)%MOD;
	}
	
	//printf("expr res %d\n",res);
	return res;	
}

int term(int& t,int& b,int& l,int& r){
	pospr("term");
	int res = factor(t,b,l,r);
	if(r<l+3)return res; // if l.r
	
	bool vd = true;
	int ind = 0;
	reg(y,t,b){
		if(s[y][l]!='.' || s[y][l+2]!='.'){
			vd = false;
		}
		if(s[y][l+1]!='.'){
			if(ind!=0)vd = false;
			else if(s[y][l+1]=='*')ind = 1;
			else vd = false;
		}
	}
	if(ind == 0 || !vd)return res;
	
	l+=3;
	res=(res*term(t,b,l,r))%MOD;
	//printf("term res %d\n",res);
	return res;
}


int factor(int& t,int& b,int& l,int& r){
	pospr("factor");
	//minus
	
	bool vd = true;
	if(r<l+2)vd=false; //if lr
	else{
		int p=0;
		reg(y,t,b){
			if(s[y][l+1]!='.')vd = false;
			if(s[y][l]=='-')p++;
			else if(s[y][l]!='.')vd = false;
		}
		if(p!=1)vd = false;
	}
	
	if(vd){
		l+=2;
		int res=factor(t,b,l,r);
		return (MOD*5-(res%MOD))%MOD;
	}
	
	
	//frac
	
	bool isfrac=true;
	int fracy = -1;
	reg(y,t,b){
		if(s[y][l]=='-'){
			if(fracy==-1)fracy=y;
			else fracy = -2;
		}
		else if(s[y][l]!='.')isfrac=false;
	}
	if(fracy!=-1 && isfrac){
		int tl=l;
		reg(x,l,r){
			if(s[fracy][x]!='-')break;
			tl = x;
		}
		int res = fraction(t,b,l,tl);
		l = tl+1;
		return res;
	}
	
	//powexpr
	return powexpr(t,b,l,r);
}


int powexpr(int& t,int& b,int& l,int& r){
	pospr("powexp");
	int res = primary(t,b,l,r);
	int pn = -1;
	reg(y,t,b){
		char c = s[y][l];
		if(c=='.')continue;
		if('0'<=c && c<='9'){
			pn = c-'0';
			break;
		}
		else return res;
	}
	if(pn==-1)return res;
	l++;
	int nr = res;
	res = 1;
	rep(i,pn){
		res=(res*nr)%MOD;
	}
	return res;
}


int primary(int& t,int& b,int& l,int& r){
	pospr("primary");
	//kakko
	bool isk = false;
	
	reg(y,t,b){
		char c = s[y][l];
		if(c=='.')continue;
		if(c=='('){
			l+=2;
			int res = expr(t,b,l,r);
			l+=2;
			return res;
		}
		if('0'<=c && c<='9'){
			l++;
			return c-'0';
		}
	}
	err("primary");
	exit(0);
}


void cutside(int& t,int& b,int& l,int& r){
	for(;;){
		bool bl = false;
		reg(y,t,b){
			if(s[y][l]!='.')bl=true;
		}
		if(bl)break;
		l++;
	}
	
	for(;;){
		bool bl = false;
		reg(y,t,b){
			if(s[y][r]!='.')bl=true;
		}
		if(bl)break;
		r--;
	}	
}

int fraction(int t,int b,int l,int r){
	pospr("fraction");
	int nt,nb,nl,nr,nv=-1;
	reg(y,t,b){
		if(s[y][l]=='-'){
			nv=y;
			break;
		}
	}
	if(nv==-1){
		err("fraction");
		exit(0);
	}
	
	nt=t; nb=nv-1; nl=l+1; nr=r-1;
	cutside(nt,nb,nl,nr);
	int divee = expr(nt,nb,nl,nr);

	nt=nv+1; nb=b; nl=l+1; nr=r-1;
	cutside(nt,nb,nl,nr);
	int diver = expr(nt,nb,nl,nr);

	return (divee*modinv(diver))%MOD;
}







int main(void){
	while(1){
		scanf("%d",&h);
		if(h==0)break;
		rep(i,h)scanf("%s",s[i]);
		w = strlen(s[0]);
		int t=0,b=h-1,l=0,r=w-1;
		printf("%d\n",expr(t,b,l,r));
	}
	return 0;
}


/*
1
2.+.3.*.5.-.7.+.9

->19;
*/











/*

 3 + 4 * - 2
-------------
         2
  - 1 - 2


.3.+.4.*.-.2.
-------------
..........2..
...-.1.-.2...


........4...2..........
(.1.-.----.)..*.-.5.+.6
........2..............
.......3...............

*/

Measured memory_bytes: 1036