#include<bits/stdc++.h>
#define FULL(x,y) memset(x,y,sizeof(x))
#define ll long long
#define SZ(x) (int)x.size()
#define pb push_back
using namespace std;

const int N=5005;
const ll mod=998244353;
ll n,m;
ll f[N][N],ff[N];

int main() {
	cin>>n>>m;
	for(ll i=0;i<=m;i++) f[i][0]=1;
	for(ll i=1;i<=m;i++) {
		for(ll j=1;j<=n;j++) {
			f[i][j]=(f[i][j-1]*i)%mod;
		}
	}
	ll ans=(n*f[m][n])%mod;
	for(ll i=0;i<=n;i++) {
		ll tmp=0;
		for(ll j=1;j<=m;j++) {
			tmp=(tmp+f[m-j][i])%mod;
		}
		ff[i]=tmp;
	}
	for(ll i=1;i<=n;i++) {
		for(ll j=1;j<i;j++) {
			ll tmp=ff[i-j-1];
			tmp=(tmp*f[m][n-i+j-1])%mod;
			ans=(ans-tmp+mod)%mod;
		}
	}
	cout<<ans<<endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

template<class D>
vector<D> dijkstra(const vector<vector<pair<int,D>>>& G, int s = 0){	//G:(v,cost)
	using P = pair<D,int>;
	priority_queue<P,vector<P>,greater<P> > que;
	int N = G.size();

	D inf = 1e9;
	vector<D> d(N,inf);
	d[s] = 0;
	que.push(P(0,s));

	while(!que.empty()){
		P p=que.top();
		que.pop();
		int v = p.sc;
		D c = p.fs;
		if(d[v]!=c) continue;
		for(auto p:G[v]){
			int to=p.fs;
			if(d[to]>d[v]+p.sc){
				d[to]=d[v]+p.sc;
				que.push(P(d[to],to));
			}
		}
	}

	return d;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int H,W; cin >> H >> W;
	VV<int> A(H,V<int>(W-1)); rep(i,H) rep(j,W-1) cin >> A[i][j];
	VV<int> B(H-1,V<int>(W)); rep(i,H-1) rep(j,W) cin >> B[i][j];
	auto id = [&](int x,int y){return x*W+y;};
	auto is = [&](int x,int y){return 0<=x&&x<H&&0<=y&&y<W;};

	using P = pair<int,int>;
	int X = H*W * 2;
	VV<P> G(X);
	auto add_edge = [&](int x,int y,int c){
		G[x].pb(y,c);
	};

	rep(i,H) rep(j,W){
		shows(i,j);
		int v = id(i,j);
		{
			int ni = i, nj = j-1;
			if(is(ni,nj)){
				int nv = id(ni,nj);
				int c = A[i][j-1];
				add_edge(v,nv,c);
			}
		}
		{
			int ni = i, nj = j+1;
			if(is(ni,nj)){
				int nv = id(ni,nj);
				int c = A[i][j];
				add_edge(v,nv,c);
			}
		}
		{
			int ni = i+1, nj = j;
			if(is(ni,nj)){
				int nv = id(ni,nj);
				int c = B[i][j];
				add_edge(v,nv,c);
			}
		}
		add_edge(v,v+H*W,1);
		add_edge(v+H*W,v,0);
		if(i){
			add_edge(v+H*W,id(i-1,j)+H*W,1);
		}
	}
	cout << dijkstra(G,0)[H*W-1] << endl;
}
