#include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
typedef vector<vector<int> > VVI;
const int INF = 1e9;

vector<string> S;
int W, H;

int MOD=2011;


int extgcd(int a, int b, int &x, int &y)
{
	int d = a;
	if (b != 0) {
		d = extgcd(b, a%b, y, x);
		y -= (a/b) * x;
	} else {
		x=1; y=0;
	}
	return d;
}

int mod_inverse(int a)
{
	int x, y;
	extgcd(a, MOD, x, y);
	return (MOD + x%MOD) % MOD;
}

int pow_mod(int val, int e)
{
	int res = 1;
	for (int i=0; i<e; i++) {
		res *= val;
		res %= MOD;
	}
	return res;
}

int expr(int &lx, int &ly, int &hx, int &hy);

int find_base(int lx, int ly, int hx, int hy)
{
	int res=-1;
	for (int i=ly; i<hy; i++) {
		if (S[i][lx] != '.') {
			if (res != -1) return -1;
			res = i;
		}
	}
	return res;
}

int factor(int &lx, int &ly, int &hx, int &hy)
{
	//printf("factor(%d, %d, %d, %d)\n", lx, ly, hx, hy);
	
	int res = 0;
	int base = -1;
	while (lx<hx && (base=find_base(lx, ly, hx, hy)) < 0) lx++;
	
	if (S[base][lx] == '-' && S[base][lx+1] == '.') {
		lx += 2; // skip "-."
		res = -factor(lx, ly, hx, hy);
		res += MOD;
		res %= MOD;
	} else if (S[base][lx] == '-' && S[base][lx+1] == '-') {
		//cerr << "fraction expr" << endl;
		// fraction a/b
		int len=0;
		while (S[base][lx+len] == '-') len++;
		int tlx=lx, tly=ly, thx=lx+len, thy=base;
		int a = expr(tlx, tly, thx, thy);
		tlx=lx, tly=base+1, thx=lx+len, thy=hy;
		int b = mod_inverse(expr(tlx, tly, thx, thy));
		res = a*b;
		res %= MOD;
		
		lx += len;
	} else {
		//cerr << "pow expr" << endl;
		// pow expr
		int primary;
		if (S[base][lx] == '(') {
			lx++; // skip "(."
			primary = expr(lx, ly, hx, hy);
			lx++; // skip ".)"
		} else {
			if (!isdigit(S[base][lx])) {
				cerr << "Error at LINE " << __LINE__ << endl;
				cerr << "expected number, but S[base][lx] = " << S[base][lx] << endl;
			}
			primary = S[base][lx] - '0';
			lx++;
		}
		
		if (base != ly && isdigit(S[base-1][lx])) {
			int e = S[base-1][lx]-'0';
			lx++; // skip digit
			res = pow_mod(primary, e);
			//cerr << primary << "^" << e << "=" << res << endl;
		} else {
			res = primary;
		}
	}

	return res;
}

int term(int &lx, int &ly, int &hx, int &hy)
{
	//printf("term(%d, %d, %d, %d)\n", lx, ly, hx, hy);
	while (lx<hx && find_base(lx, ly, hx, hy) < 0) lx++;
	int res = factor(lx, ly, hx, hy);
	
	while (lx < hx && ly < hy) {
		int base=-1;
		while (lx<hx && (base=find_base(lx, ly, hx, hy)) < 0) lx++;
		if (lx < hx-1 && S[base][lx] == '*' && S[base][lx+1] == '.') {
			lx += 2; // skip "*."
			res *= factor(lx, ly, hx, hy);
			res %= MOD;
		} else {
			break;
		}
	}
	
	return res;
}

int expr(int &lx, int &ly, int &hx, int &hy)
{
	//printf("expr(%d, %d, %d, %d)\n", lx, ly, hx, hy);
	while (lx<hx && find_base(lx, ly, hx, hy) < 0) lx++;
	int res = term(lx, ly, hx, hy);
	
	while (lx < hx && ly < hy) {
		int base=-1;
		while (lx<hx && (base=find_base(lx, ly, hx, hy)) < 0) lx++;
		if (S[base][lx]=='+') {
			lx += 2; // skip "+."
			res += term(lx, ly, hx, hy);
			res %= MOD;
		} else if (S[base][lx]=='-') {
			lx += 2; // skip "-."
			res -= term(lx, ly, hx, hy);
			res += MOD;
			res %= MOD;
		} else {
			break;
		}
	}
	
	return res;
}

int main()
{
	int N;
	while (cin>>N, N) {
		cin.ignore();
		S.clear();
		rep(i, N) {
			string s; getline(cin, s);
			S.push_back(s);
		}
		int lx=0, ly=0, hx=(int)S[0].size(), hy=N;
		cout << expr(lx, ly, hx, hy) << endl;
	}
}

Measured memory_bytes: 1224