#include<bits/stdc++.h>
using namespace std;

using ll = long long;

const int MOD = 1e9 + 7;

void E(){
     int n;
      cin>>n;
      vector<int> elems;
      ll sum = 0;
      ll res = 0;
      for(int i = 0; i < n; i++){
          int elem;
          cin>>elem;
          sum += elem;
          elems.push_back(elem);
      }  
      for(int i = 0; i < n; i++){
          sum -= elems[i];
          res = (res%MOD + (elems[i]%MOD * sum%MOD))%MOD;
      }
      cout<<res<<endl;
}
void F(){
    int n, m;
    cin>>n>>m;
    int mn = 1e9;
    int grid[n][m];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            int elem;
            cin>>elem;
            mn = min(mn, elem);
            grid[i][j] = elem;
        }
    }
    int res = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            res += grid[i][j] - mn;
        }
    }
    cout<<res<<endl;

}
int main(){
      F();
}
#include<bits/stdc++.h>
#define ll long long
#define mod 998244353ll
using namespace std;
ll mem[5003][5003], inv;
char str[5003][5003];
int n, m;
ll dp(int x, int y)
{
    if(x==n-1 && y==m-1)return 1;
    if(x==n || y==m)return 0;
    if(mem[x][y]!=-1)return mem[x][y];

    if(!str[x][y])mem[x][y]= (((2ll*(dp(x+1, y)+dp(x, y+1)))%mod)*inv)%mod;
    else
    {
        if(str[x][y]=='R')mem[x][y]= dp(x, y+1);
        else if(str[x][y]=='D')mem[x][y]= dp(x+1, y);
        else mem[x][y]= (dp(x+1, y)+dp(x, y+1))%mod;
    }

    return mem[x][y];
}
ll bigmod(int b, int p)
{
    if(!p)return 1;
    ll ret= bigmod(b, p/2);
    ret= (ret*ret)%mod;
    if(p&1ll)ret= (ret*b)%mod;
    return ret;
}
int main()
{
    inv= bigmod(3, mod-2);

    int k;
    scanf("%d %d %d", &n, &m, &k);

    ll external= 1ll;
    for(int i=0; i<(n*m)-k; i++)
    external= (3ll*external)%mod;

    while(k--)
    {
        char c;
        int x, y;
        scanf("%d %d %c", &x, &y, &c);
        str[x-1][y-1]= c;
    }

    memset(mem, -1, sizeof mem);
    printf("%lld\n", (external*dp(0, 0))%mod);

    return 0;
}
