import java.util.*;

public class Main {

    static class Cell{
        int x,y;
        Cell(int x,int y){
            this.x=x;
            this.y=y;
        }
    }

    static final int MAX=1000000;
    static final long MOD=1000000007L;

    static int[][] grid;
    static int n,m;

    static int[][] comp;
    static boolean[][] vis;

    static int[] dx={1,-1,0,0};
    static int[] dy={0,0,1,-1};

    static ArrayList<ArrayList<Cell>> components=new ArrayList<>();

    static void bfs(int sx,int sy,int id){

        Queue<Cell> q=new LinkedList<>();

        q.add(new Cell(sx,sy));

        vis[sx][sy]=true;

        comp[sx][sy]=id;

        ArrayList<Cell> list=new ArrayList<>();

        while(!q.isEmpty()){

            Cell cur=q.poll();

            list.add(cur);

            for(int k=0;k<4;k++){

                int nx=cur.x+dx[k];
                int ny=cur.y+dy[k];

                if(nx>=0 && ny>=0 && nx<n && ny<m){

                    if(grid[nx][ny]>0 && !vis[nx][ny]){

                        vis[nx][ny]=true;

                        comp[nx][ny]=id;

                        q.add(new Cell(nx,ny));

                    }
                }
            }

        }

        components.add(list);

    }

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        n=sc.nextInt();
        m=sc.nextInt();

        grid=new int[n][m];

        comp=new int[n][m];

        vis=new boolean[n][m];

        HashMap<Integer,Integer> freq=new HashMap<>();

        for(int i=0;i<n;i++){

            for(int j=0;j<m;j++){

                grid[i][j]=sc.nextInt();

                if(grid[i][j]>0){

                    freq.put(grid[i][j],
                            freq.getOrDefault(grid[i][j],0)+1);

                }

            }

        }

        int id=0;

        for(int i=0;i<n;i++){

            for(int j=0;j<m;j++){

                if(grid[i][j]>0 && !vis[i][j]){

                    bfs(i,j,id++);

                }

            }

        }

        long finalAnswer=0;

        for(ArrayList<Cell> component:components){

            // Remove current component
            for(Cell c:component){

                int val=grid[c.x][c.y];

                freq.put(val,freq.get(val)-1);

                if(freq.get(val)==0)
                    freq.remove(val);

            }

            // Calculate sed values
            for(Cell c:component){

                int val=grid[c.x][c.y];

                long sed=0;

                for(int multiple=val;
                    multiple<=MAX;
                    multiple+=val){

                    Integer f=freq.get(multiple);

                    if(f!=null){

                        sed += (long)multiple*f;

                    }

                }

                finalAnswer=(finalAnswer+sed)%MOD;

            }

            // Restore component
            for(Cell c:component){

                int val=grid[c.x][c.y];

                freq.put(val,
                        freq.getOrDefault(val,0)+1);

            }

        }

        System.out.println(finalAnswer%MOD);

    }

}